Escolar Documentos
Profissional Documentos
Cultura Documentos
ASET
Contents
ASET
ASET
Primitive Data types byte, short, int, and long for integer values of various sizes float and double for real (rational) values of differing accuracy boolean for logical (true/false) values char for individual characters
ASET
Primitives sizes and Default Values PRIMITIVE long int short byte float double char bool (boolean in Java) SIZE IN BITS 8 bytes 4 bytes 2 bytes 1 bytes 8 bytes 4 bytes 2 bytes (unicode) 1 bytes Default Values 0L 0 0 0 0.0f 0.0d \u0000 false
ASET
Literals Examples Integers 4, 19, -5, 0, 1000 Doubles 3.14, 0.0, -16.123 Strings Hi Mom Enter the number : Character 'A' 'X' '9' '$' '\n' Boolean true, false
ASET
Identifiers An identifier is a sequence of characters that consist of letters, digits, underscores (_), and dollar signs ($). Identifier = the technical term for a name in a programming language Identifier naming rules: The first character must not be a digit. May begin with a letter or the underline character _ If these rules are broken, your program won't compile. Must not be a Java keyword Names must be descriptive.
ASET
Identifier examples class name identifier: Hello method name identifier: main variable name identifier: height Constants All caps with _ between words
ASET
Variables A variables can be considered as a name given to the location in memory where values are stored. How does the computer know which type of data a particular variable can hold? Before a variable is used, its type must be declared in a declaration statement. Declaration statement syntax: <type> <list of variables separated by commas>;
12
ASET
Variable Declaration Syntax Syntax: type variable_name; or type variable_name = expression; Note type must be known to the compiler variable_name must be a valid identifier expression is evaluated and assigned to variable_name location In the first form, a default value is given (0, false, or null, depending on type)
ASET
Comments There are two types of comments used in Java. These are: In Java, comments are preceded by two slashes (//) in a line Or enclosed between /* and */ in one or multiple lines. When the compiler sees //, it ignores all text after // in the same line. When it sees /*, it scans for the next */ and ignores any text between /* and */.
ASET
Reserved Words Reserved words or keywords are words that have a specific meaning to the compiler and cannot be used for other purposes in the program.
For example, when the compiler sees the word class, it understands that the word after class is the name for the class.
Other reserved words are like public, static, and void. Their use will be introduced later in the book.
12
ASET
Java Keywords
Keywords are words reserved for Java and cannot be used as identifiers or variable names
Jav K w ds a ey or
abstract catch do final implements long private static throw void const boolean char double finally import native protected super throws volatile goto break class else float instanceof new public switch transient while byte continue extends for int null return synchronized true case default false if interface package short this try
ASET
Operators Types of operators Simple Assignment Operator = (Simple assignment operator) Arithmetic Operators + (Additive operator (also used for String concatenation)) - (Subtraction operator) * (Multiplication operator) / (Division operator) % (Remainder operator)
14
ASET
Operators cont Unary Operators + (Unary plus operator; indicates positive value) - (Unary minus operator; negates an expression) ++ (Increment operator; increments a value by 1) -- (Decrement operator; decrements a value by 1) ! (Logical compliment operator; inverts the value of a boolean) Equality and Relational Operators
ASET
Operators cont
Conditional Operators && (Conditional-AND) || (Conditional-OR) ?: (Ternary (shorthand for if-then-else statement)) Type Comparison Operator instanceof (Compares an object to a specified type)
16
ASET
Operators cont Bitwise and Bit Shift Operators ~ (Unary bitwise complement) << (Signed left shift) >> (Signed right shift) >>> (Unsigned right shift) & (Bitwise AND) ^ (Bitwise exclusive OR) Operand should be integer type
17
ASET
Operators cont
Bitwise AND 10012 & 00112 = 00012 Bit OR 10012 | 00112 = 10112 Exclusive OR 10012 ^ 00112 = 10102 1s Complement ~ 000010102 = 111101012
ASET
Shift right(>>)
x >> y = x //2yy x >> y = x 2
Unsigned shift right(>>>) Give this operator because Java does not support unsigned integer.
ASET
<<
Left
Right
>>
a a >> 2 b b >> 2
3 0 -4 -1
ASET
Precedence Operator
( ) ++ * + < == & ^ | && || ?: = += -= *= /= <= != / > >= -%
Associativity
From left to right From right to left From left to right From left to right From left to right From left to right From left to right From left to right From left to right From left to right From left to right From right to left From right to left
High
Low
ASET
THANK YOU
22