0% found this document useful (0 votes)
2 views

2-java

Uploaded by

dhaouadi.eya
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

2-java

Uploaded by

dhaouadi.eya
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

DATA TYPES:

-----------------------------------------------------------------------------------
--------
Primitive data types: store simple values. Not composed of other data types.
•8:(byte-short-int-long-floar-double-char-boolean)

->use "==","!=",">","<","<=",">=" for comparison

-----------------------------------------------------------
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

♦when operation contains integers and doubles: result is double

-----------------------------------------------------------------------------------
--------

TYPE: char(' '):

char letter ='a';


System.out.println(letter); --> a

•access a char in a string: stringname.charAt(index);

•char <- ASCII value:(int)


'a' + 10 -> 107
(char)('a' + 2) -> 'c'

-----------------------------------------------------------------------------------
--------

TYPE: String (" "): ('class' by default + 'object')


•string = array of char
--object is an implementation/initialisation of classes--

•contains methods: s.toUpperCase() / s.length() /...

•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()"

-----------------------------------------------------------------------------------
--------

VARIABLES:(have name + type + stored value)

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.

-->Declaration + Initialization: (in one statement)


type name = value; --> int x = 3;

3.Usage:
System.out.println("x is "+ x ); --> "x is 3"

[Compiler ERRORS: x has no value / x already exists / incompatible types]

-----------------------------------------------------------------------------------
--------

Formatting text:

System.out.printf("format string", parameters);

•printf: create PLACEHOLDERS inside the string (instead of + concatination)


%d: integer
%f: real number
%s: string

•\n : drop to next line.

------------------
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
------------------

•width: (minimum nbr of digits/characters)


%Wd: W characters wide, rigth-aligned
%-Wd: W characters wide, left-aligned

•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

You might also like