JavaSyntax

Програмиране и програмни езици Лекция

Слайд 1

Java Syntax
Data Types, Variables, Operators, Expressions, Statements, Console I/O, Conditional Statements
SoftUni Team
Technical Trainers
Software University
http://softuni.bg

Слайд 2

Primitive Data Types
Variables
Operators and Expressions
Console-Based Input and Output
Conditional Statements
Loops
Methods
Table of Contents

Слайд 3

Primitive Data Types in Java

Слайд 4

Computers are machines that process data
Data is stored in the computer memory in variables
Variables have name, data type and value
Example of variable definition and assignment in Java

When processed, data is stored back into variables
How Computing Works?
int count = 5;
Data type
Variable name
Variable value

Слайд 5

A data type:
Is a domain of values of similar characteristics
Defines the type of information stored in the
computer memory (in a variable)
Examples:
Positive integers: 1, 2, 3, …
Alphabetical characters: a, b, c, …
Days of week: Monday, Tuesday, …
What Is a Data Type?

Слайд 6

A data type has:
Name (Java keyword, e.g. int)
Size (how much memory is used)
Default value
Example:
Integer numbers in C#
Name: int
Size: 32 bits (4 bytes)
Default value: 0
Data Type Characteristics
int: sequence of 32 bits in the memory
int: 4 sequential bytes in the memory

Слайд 7

byte (-128 to 127): signed 8-bit
short (-32,768 to 32,767): signed 16-bit
int (-2,147,483,648 to 2,147,483,647): signed 32-bit
long (-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807): signed 64-bit
Integer Types
byte b = 1;
int i = 5;
long num = 3L;
short sum = (short) (b + i + num);

Слайд 8

Integer Types
Live Demo

Слайд 9

Floating-point types are:
float (±1.5 × 10−45 to ±3.4 × 1038)
32-bits, precision of 7 digits
double (±5.0 × 10−324 to ±1.7 × 10308)
64-bits, precision of 15-16 digits
The default value of floating-point types:
Is 0.0F for the float type
Is 0.0D for the double type
Floating-Point Types
double
float

Слайд 10

Floating-Point Types – Examples
float f = 0.33f;
double d = 1.67;
double sum = f + d;
float fSum = f + d; // This will not compile
double infinity = 3.14 / 0;

System.out.println(f); // 0.33
System.out.println(d); // 1.67
System.out.println(sum); // 2.000000013113022
System.out.println(infinity); // Infinity

Слайд 11

The floating-point arithmetic sometime works incorrectly
Don't use float and double for financial calculations!
In Java use the BigDecimal class for financial calculations:
BigDecimal
import java.math.BigDecimal;

BigDecimal bigF = new BigDecimal("0.33");
BigDecimal bigD = new BigDecimal("1.67");
BigDecimal bigSum = bigF.add(bigD);
System.out.println(bigSum); // 2.00

Слайд 12

Floating-Point and BigDecimal Types
Live Demo

Слайд 13

Boolean
Other Primitive Data Types
boolean b = true;
System.out.println(b); // true
System.out.println(!b); // false
Character
ch = 'ю';
System.out.println(ch);
ch = '\u03A9'; \\ Ω
System.out.println(ch);
Enable Unicode in the Eclipse console

Слайд 14

Boolean and Character Types
char
Live Demo
false
true

Слайд 15

The String data type:
Declared by the String class
Represents a sequence of characters
Has a default value null (no value)
Strings are enclosed in quotes:

Strings can be concatenated
Using the + operator
The String Data Type
String s = "Hello, Java";

Слайд 16

Concatenating the names of a person to obtain the full name:

We can concatenate strings and numbers as well:
Saying Hello – Example
String firstName = "Ivan";
String lastName = "Ivanov";
System.out.println("Hello, " + firstName);

String fullName = firstName + " " + lastName;
System.out.println("Your full name is: " + fullName);
int age = 21;
System.out.println("Hello, I am " + age + " years old");

Слайд 17

String Type
Live Demo
string

Слайд 18

The Object type:
Is declared by the java.lang.Object class
Is the base type of all other types
Can hold values of any type
The Object Type
Object
object dataContainer = 5;
System.out.print("The value of dataContainer is: ");
System.out.println(dataContainer);
dataContainer = "Five";
System.out.print("The value of dataContainer is: ");
System.out.println(dataContainer);

Слайд 19

Objects
Live Demo
Object

Слайд 20

Variables, Identifiers, Literals
"\uE52B"
"\t\r\n\r\n\r\n"
6.02e+23
null
true
0xFE

Слайд 21

When declaring a variable we:
Specify its type
Specify its name (called identifier)
May give it an initial value
The syntax is the following:

Example:
Declaring Variables
<data_type> <identifier> [= <initialization>];
int height = 200;

Слайд 22

Variables in Java live inside their { } scope:
Variable Scope
int sum = 0;
for (int i = 0; i < 10; i++) {
System.out.println(i);
sum += i;
int temp = 2*i;
}

System.out.println(sum); // sum will be printed here
System.out.println(i); // Error: i is out of scope here
System.out.println(temp); // Error: temp is out of scope here

Слайд 23

Identifiers may consist of:
Letters (Unicode)
Digits [0-9]
Underscore "_"
Examples: count, firstName, Page, брояч, 计数器
Identifiers
Can begin only with a letter or an underscore
Cannot be a Java keyword (like int or class)
Identifiers

Слайд 24

Literals are the representations of values in the source code
Literals in Java
int dec = 5; // decimal value 5
int hex = 0xFE; // hexadecimal value FE -> 254
int bin = 0b11001; // binary value 11001 -> 25
int bigNum = 1_250_000; // decimal value 1250000
long num = 1234567890123456789L;
long hexNum = 0x7FFF_FFFF_FFFF_FFFFL;
boolean bool = true;
float floatNum = 1.25e+7f; // 12500000
double doubleNum = 6.02e+23; // 602000000000000000000000
char newLine = '\n'; // Character <new line>
char unicodeChar = '\u00F1'; // Character: ñ
long fourBytes = 0b11010010_01101001_10010100_10010010; // -764832622
String str = "Hello,\nI\'m Java.";

Слайд 25

Each primitive type in Java has a corresponding wrapper:
int  java.lang.Integer
double  java.lang.Double
boolean  java.lang.Boolean
Primitive wrappers can have a value or be null (no value)
Nullable Types: Integer, Long, Boolean, …
Integer i = 5; // Integer value: 5
i = i + 1; // Integer value: 6
i = null; // No value (null)
i = i + 1; // NullPointerException

Слайд 26

Operators and Expressions in Java
int bit = (n & (1 << p)) >> p;
n = n & (~(1<<p)) | (bit<<p);

Слайд 27

Operator is an operation performed over data at runtime
Takes one or more arguments (operands)
Produces a new value
Example of operators:
Operators have precedence
Precedence defines which will be evaluated first
Expressions are sequences of operators and operands that are evaluated to a single value, e.g. (a + b) / 2
What is an Operator?
a = b + c;
Operator "+"
Operator "="

Слайд 28

Operators in Java

Слайд 29

Operators Precedence

Слайд 30

Parenthesis operator always has the highest precedence
Note: prefer using parentheses to avoid ambiguity

Operators Precedence (2)

Преглед на началото - целият файл след изтегляне

Описание

Дисциплина: Програмиране

0 коментара

Все още няма коментари. Бъдете първият, който ще коментира.

За да коментирате, трябва да сте влезли в профила си.

Влезте