1.Instance Variables
Variables declared inside a class & out side a method without static keyword. Non-static fields are also known as instance variables because their values are unique to each instance of a class
public class Demo
{
int count = 20; //1 - Instance variable
}
2.Static Variables
Also know as class variables. It is any field declared with the static modifier. It means that there is exactly one copy of this variable in existence, regardless of how many times the class has been instantiated.
public class VariableExample
{
static float PI = 3.14f; //2 - Class variable
}
3.Local variables
These are used inside methods as temporary variables exist during the method execution. Local variables are only visible to the methods in which they are declared; they are not accessible from the rest of the class.
public class Demo
{
public static void main( String[] args ) {
int age = 30; //3 - Local variable (inside method body)
}
}
Operators
System.out.print(9 / 3); // Outputs 3
System.out.print(9 % 3); // Outputs 0
System.out.print(10 / 3); // Outputs 3
System.out.print(10 % 3); // Outputs 1
System.out.print(11 / 3); // Outputs 3
System.out.print(11 % 3); // Outputs 2
System.out.print(12 / 3); // Outputs 4
System.out.print(12 % 3); // Outputs 0
Numeric Promotion Rules
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 to 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 operands is int.
4. After all promotion has occurred and the operands have the same data type, the resulting value will have the same data type as its promoted operands.
What is the data type of x / y?
short x = 10;
short y = 3;
In this case, we must apply the third rule, namely that x and y will both be promoted to int before the operation, resulting in an output of type int.
What is the data type of x * y / z?
short x = 14;
float y = 13;
double z = 30;
we evaluate the multiple and division from left-to-right.In this case, we must apply all of the rules. First, x will automatically be promoted to int solely because it is a short and it is being used in an arithmetic binary operation. The promoted x value will then be automatically promoted to a float so that it can be multiplied with y. The result of x * y will then be automatically.
1.Increment/ Decement
How this following expression is evaluated?
int x = 3;
int y = ++x * 5 / x-- + --x;
System.out.println("x is " + x);
System.out.println("y is " + y);
int y = 4 * 5 / x-- + --x; // x assigned value of 4
int y = 4 * 5 / 4 + --x; // x assigned value of 3
int y = 4 * 5 / 4 + 2; // x assigned value of 2
we evaluate the multiple and division from left-to-right, and finish with the addition.The result is then printed: x is 2 & y is 7
Does it work?
long t = 192301398193810323; // DOES NOT COMPILE
It does not compile because Java interprets the literal as an int and notices that the value is larger than int allows. The literal would need a postfi x L to be considered a long
short x = 10;
short y = 3;
short z = x * y; // DOES NOT COMPILE
short x = 10;
short y = 3;
short z = (short)(x * y);
long x = 10;
int y = 5;
y = y * x; // DOES NOT COMPILE
Based on the last two sections, you should be able to spot the problem in the last line.This last line could be fi xed with an explicit cast to (int), but there’s a better way using the compound assignment operator:
long x = 10;
int y = 5;
y *= x;
The compound operator will first cast x to a long, apply the multiplication of two long values, and then cast the result to an int.
long x = 5;
long y = (x=3);
System.out.println(x); // Outputs 3
System.out.println(y); // Also, outputs 3
Equality Operators (==)
The comparisons for equality are limited to these three cases, so you cannot mix and match types. For example, each of the following would result in a compiler error:
boolean x = true == 3; // DOES NOT COMPILE
boolean y = false != "Giraffe"; // DOES NOT COMPILE
boolean z = 3 == "Kangaroo"; // DOES NOT COMPILE
Conditional Statements
int x = 1;
if(x) { // DOES NOT COMPILE
...
}
int x = 1;
if(x = 5) { // DOES NOT COMPILE
...
}
System.out.println((y > 5) ? 21 : "Zebra");
int animal = (y < 91) ? 9 : "Horse"; // DOES NOT COMPILE
^ (X-OR) - Homogenious are FALSE(T,T F,F), Hertrogenious are TRUE(T,F F,T)
0 Comments
Post a Comment