2-java
2-java
-----------------------------------------------------------------------------------
--------
Primitive data types: store simple values. Not composed of other data types.
•8:(byte-short-int-long-floar-double-char-boolean)
-----------------------------------------------------------
Operators:(+,-,*,/,%)
TYPE: integer(n):
•System.out.println(3*4) -> 12
•int/int=int --> 5/2=2 not 2.5 --(int/0=error)--
•int%int=remainder from division --> 7%2=1
--------------
TYPE: double(n.0):
•work with all operators
•double/double=double(exact answer) --> 5.0/2.0=2.5
-----------------------------------------------------------------------------------
--------
-----------------------------------------------------------------------------------
--------
•concatenation:(+)
"abc"+1+2 -> "abc12"
1+2+"abc" -> "3abc"
"abc"+4*2 -> "abc8"
"1" + 1 ->"11"
System.out.println("grade: "+(80.5+70.5)/2) -> "grade: 75.5"
•comparison:
name1="abc"
name2="abc"
-> the 2 strings have 2 different references to the memory. Comparison with "=="
gives difference between the 2 strings.
-> comparison "==": compare references. (for objects only)
-> test/comparison methods: "equals()", "equalsIgnoreCase()", startsWith()",
"endsWith()", "contains()","compareTo()"
-----------------------------------------------------------------------------------
--------
1.Declaration:
type name; --> example: ( int x; )
[name=identifier]
2.Assignment/Initialization:
name = expression; --> example: ( x = 3; ) or ( x = 2 + 1; )
•you can assign a value more than once.
3.Usage:
System.out.println("x is "+ x ); --> "x is 3"
-----------------------------------------------------------------------------------
--------
Formatting text:
------------------
int x=3;
int y=5;
System.out.printf("x is %d and y is %d \n", x, y); --> x is 3 and y is 5
------------------
•precision:
%W.Df: W chars wide, rounded to D digits after decimal
-----------------------------------------------------------------------------------
--------
INCREMENT/DECREMENT:
•variable++
•variable--
MODIFY & ASSIGN:
•variable += value •variable *= value
•variable -= value •variable /= value