Sei sulla pagina 1di 32

JAVA Basics : Variable and Datatype in Java

Variable:
int data=10;//Here data is variable

JAVA Basics : Variable and Datatype in Java


Types of Variable: There are three types of variables in java
Local Variable :
Instance Variable:
Static variable:

JAVA Basics : Variable and Datatype in Java


Types of Variable
class A
{
int data=50; // instance variable
static int m=100; //static variable
void method()
{
int n=90; //local variable
}
}//end of class

JAVA Basics : Variable and Datatype in Java


Datatype : In java, there are two types of data types
primitive data types
non-primitive data types

JAVA Basics : Variable and Datatype in Java

JAVA Basics : Literals in Java

Integer literals
Floating literals
Character literals
String literals
Boolean literals

JAVA Basics : Literals in Java

Integer literals
Examples:

int decimal = 100;


int octal = 0144;
int hexa = 0x64;

JAVA Basics : Literals in Java

Floating literals
Examples:

0.0314 *10 (i.e 3.14).


6.5E+32 (or 6.5E32) Doubleprecision floating-point literal
7D Double-precision floating-point
literal
.01f Floating-point literal

JAVA Basics : Literals in Java

Character literals
Escape
\n
\t
\b
\r
\f
\\
\'
\"
\d
\xd
\ud

Meaning
New line
Tab
Backspace
Carriage return
Formfeed
Backslash
Single quotation mark
Double quotation mark
Octal
Hexadecimal
Unicode character

JAVA Basics : Literals in Java

String Literals

""

"\""

"This is a string"

"This is a " + "two-line string"

JAVA Basics : Literals in Java

Boolean Literals
Example
boolean chosen = true;

JAVA Basics : Type Conversion and Casting


Type Casting
Java data type casting comes with 3 flavors.
Implicit casting
Explicit casting
Boolean casting.

JAVA Basics : Type Conversion and Casting


1. Implicit casting (widening conversion)
int x = 10; // occupies 4 bytes
double y = x; // occupies 8 bytes
System.out.println(y); // prints 10.0

JAVA Basics : Type Conversion and Casting


2. Explicit casting (narrowing conversion)
double x = 10.5;
int y = x;
------------------------------double x = 10.5;
int y = (int) x;

// 8 bytes
// 4 bytes ; raises compilation error

JAVA Basics : Type Conversion and Casting


3. Boolean casting
Following raises error.
boolean x = true;
int y = x;
boolean x = true;
int y = (int) x;

// error
// error

JAVA Basics :Operators


Java provides a rich set of operators to manipulate variables:
We can divide all the Java operators into the following groups:
Arithmetic Operators
Relational Operators
Bitwise Operators
Logical Operators
Assignment Operators
Misc Operators

JAVA Basics :Operators : Arithmetic Operators


Arithmetic operators are used in mathematical expressions in the
same way that they are used in algebra

JAVA Basics :Operators : Relational Operators

JAVA Basics :Operators : Bitwise Operators


Java defines several bitwise operators, which can be applied to
the integer types, long, int, short, char, and byte.
Bitwise operator works on bits and performs bit-by-bit operation.
Assume if a = 60; and b = 13; now in binary format they will be
as follows:
a = 0011 1100
b = 0000 1101
----------------a&b = 0000 1100
a|b = 0011 1101
a^b = 0011 0001
~a = 1100 0011

JAVA Basics :Operators : Bitwise Operators

JAVA Basics :Operators : Bitwise Operators

JAVA Basics :Operators : Logical Operators

JAVA Basics :Operators : Assignment Operators

JAVA Basics :Operators : Assignment Operators

JAVA Basics :Operators : Misc Operators :


Conditional Operator ( ? : ):
Conditional operator is also known as the ternary operator. This
operator consists of three operands and is used to evaluate
Boolean expressions. The goal of the operator is to decide which
value should be assigned to the variable. The operator is written
as:
variable x = (expression) ? value if true : value
if false

JAVA Basics :Operators : Misc Operators :


Conditional Operator ( ? : ):
public class Test {
public static void main(String args[]){
int a , b;
a = 10;
b = (a == 1) ? 20: 30;
System.out.println( "Value of b is : " + b );
b = (a == 10) ? 20: 30;
System.out.println( "Value of b is : " + b );
}
}
Output :
Value of b is : 30
Value of b is : 20

JAVA Basics :Operators : Misc Operators :


instanceof Operator:
This operator is used only for object reference variables. The
operator checks whether the object is of a particular type(class
type or interface type). instanceof operator is wriiten as:
( Object reference variable ) instanceof (class/interface
type)
If the object referred by the variable on the left side of the
operator passes the IS-A check for the class/interface type on
the right side, then the result will be true.

JAVA Basics :Operators : Misc Operators :


instanceof Operator:
public class Test {

class Vehicle {}

public static void main(String args[]){ public class Car extends Vehicle {
String name = "James";
public static void main(String
*/following will return true since
args[]){
name is type of String*/
Vehicle a = new Car();
boolean result = name instanceof
boolean result = a instanceof
String;
Car;
System.out.println( result );
System.out.println( result );
}
}
}
}
Out of both exmple
true

JAVA Basics :Operators :Precedence of Java


Operators
Operator precedence determines the grouping of terms in an
expression. This affects how an expression is evaluated. Certain
operators have higher precedence than others; for example, the
multiplication operator has higher precedence than the addition
operator:
For example, x = 7 + 3 * 2; here x is assigned 13, not 20 because
operator * has higher precedence than +, so it first gets multiplied
with 3*2 and then adds into 7.
Here, operators with the highest precedence appear at the top of the
table, those with the lowest appear at the bottom. Within an
expression, higher precedence operators will be evaluated first.

JAVA Basics :Operators :Precedence of Java


Operators

JAVA Basics :Operators:

Output:

Thanks

Potrebbero piacerti anche