Numeric Promotion, Numeric Type Conversion(Widening, Narrowing)

 1. If two values have different data types, Java will automatically promote one of the values to the larger of the two data types. 

2. If one of the values is integral and the other is floating-point, Java will automatically promote the integral value of the floating-point value's data type. 

3. Smaller data types, namely byte, short and char, are first promoted to int any time they're used with a Java binary arithmetic operator, even if neither of the operand is int.

4. After all promotion has occurred and the operand have the same data type, the resulting value will have the same data type as its promoted operand. 


int a = 4;
System.out.println(-a);

int b = -3;
System.out.println(+b);
Outcome: -4 and -3;
Cunki musbetle menfi neticede menfi verir.

Order of operator precedence
Post-unary operators : expression++, expression--
Pre-unary operators: ++expression, --expression
Other unary operators: +, -, !
Multiplication/Division/Modulus: *, /, %
Addition/Subtraction: +, -
Shift operators: <<, >>, >>>
Relational operators: <, >, <=, >=, instanceof
Equal to/Not equal to: ==, !=
Logical operators: &, ^, |
Short-circuit logical operators &&, ||
Ternary operators

public class Main {

public static void main(String[] args) {
int b = 5;
int a = 10;

int c = a++ + b++ + (--b * 2) / a;
System.out.println(c);

int x = 3;
int y = 2;
int r = x * y + 2;
int r1 = 2 + y * x;
System.out.println(r);
System.out.println(r1);

int k = 6;
int t = 7;
int result = ++k * ++t;
System.out.println(result);

int aa = 5;
int bb = 10;
int rr = --aa * 2 / bb;
System.out.println(rr);
}
}

Conversion
1. Impossible conversion.
boolean - hec bir tipe cervire bilmir.
byte ve short ile char arasinda bir cevrilme ola bilmez cunki byte ve short iwaretlidir, lakin
char iwaretsizdir.
2. Widening conversion.
Tam ededlerden kesrli ededlere conversion olduqda informasiya ite biler.


3. Narrowing conversion.
Java narrowing conversion-u avtomatik aparmir. Proqramci ozu cast etmelidir. 

Final keyword
Java-da 4 ferqli wey final ola biler: variable, parameter, class, method. 
final int i = 5;
i = 8; // Compiler error.

blank final
final int i;
i = 8; // not a compiler error
i = -3; // Compiler error.

Комментарии

Популярные сообщения из этого блога

Lesson1: JDK, JVM, JRE

SE_21_Lesson_11: Inheritance, Polymorphism

SE_21_Lesson_9: Initialization Blocks, Wrapper types, String class