Core Java
By Dines Kumar Kushwaha
Data Types in Java
• Data types specify the different sizes and values that can be stored
in the variable. There are two types of data types in Java:
[Link] data types: The primitive data types include boolean,
char, byte, short, int, long, float and double.
[Link]-primitive data types: The non-primitive data types
include Classes, interfaces and Arrays
Data Type Default Value Default size
Java Primitive Data Types
boolean false 1 bit
char '\u0000' 2 byte
byte 0 1 byte
short 0 2 byte
int 0 4 byte
long 0L 8 byte
float 0.0f 4 byte
double 0.0d 8 byte
Variable
• A variable is the name of a reserved area allocated in
memory. In other words, it is a name of the memory
location. It is a combination of "vary + able" which means
its value can be changed.
Types of Variables
There are three types of variables in Java:
• local variable-:A variable declared inside the body of the
method is called local variable.
• instance variable-:A variable declared inside the class but
outside the body of the method, is called an instance
variable.
• static variable-: Not need to object to access.
Example
class variable
{
int a=10;//instance variable
static int b=100;//static variable
public static void main(String args[])
{
int c=300;//local varaiable
[Link](b);
[Link](c);
variable s1=new variable();//creating object of variable class.
[Link](s1.b);
}
}
How to input/read value from user/runtime
• There are following ways to read a value from run time-:
1. Command Line Argument-: passed some argument/value at the time of running the java
program.
Ex-:
class Number
{
public static void main(String rags[])
{
int n1;
n1=[Link](args[0])
[Link](“Value is=“+n);
}
}
Syntax-:To compile
c:\use\java> javac [Link]
To run
c:\use\java> java classname val1,val2,….valN
How to input/read value from user/runtime
2. Using Scanner class
The Scanner class is used to get user input, and it is located in the
[Link] package.
Note-: To use the Scanner class, create an object of the class and use any
of the available methods located in the Scanner class.
Input Type:
Method Uses
nextLine() Reads a string value from the user.
nextBoolean() Reads a Boolean value from user
nextInt() Reads a integer value from user
nextDouble() Reads a double value from user
nextLong() Reads a long value from user
Example Of Scanner class to read integer value
Import [Link];
class Number
{
public static void main(String args[])
{
int n1;
Scanner sc=new Scanner([Link])//creating object of Scanner class.
[Link](“Enter the number”);
n1=[Link]();
[Link](“Value is=“+n1);
}
}
Operator in java
• 1. Arithmetic Operators: They are used to perform simple arithmetic operations on primitive
data types.
• * : Multiplication
• / : Division
• % : Modulo
• + : Addition
• – : Subtraction
• 2. Unary Operators: Unary operators need only one operand. They are used to increment,
decrement or negate a value.
– : Unary minus, used for negating the values.
• + : Unary plus indicates the positive value
• ++ : Increment operator, used for incrementing the value by 1. There are two varieties of
increment operators.
• Post-Increment
• Pre-Increment
• Decrement operator, used for decrementing the value by 1. There are two varieties of decrement
operators.
• Post-decrement:
• Pre-Decrement:
• ! : Logical not operator
Operator in Java
3. Assignment Operator: ‘=’ Assignment operator is used to assigning
a value to any variable.
+=-: Ex: if a=9 then a+=5-: same as a=a+5
-=
*=
/=
%=
4. Relational Operators:
==, Equal to
!=, Not Equal to
<, less than:
<=, less than or equal to
>, Greater than:
>=, Greater than or equal to
Operator in Java
• 5. Logical Operators:
&&, Logical AND: returns true when both conditions are true.
• ||, Logical OR: returns true if at least one condition is true.
• !, Logical NOT: returns true when a condition is false and vice-versa
6. Ternary operator: Ternary operator is a shorthand version of the if-
else statement. It has three operands and hence the name ternary.
Syntax: (condition)? If true: if false;
• 7. Bitwise Operators: These operators are used to perform the
manipulation of individual bits of a number.
• &, Bitwise AND operator:
• |, Bitwise OR operator:
• ^, Bitwise XOR operator:
• ~, Bitwise Complement Operator:
• <<, Left shift operator:
• >>, right shift operator:
Java Control Statements | Control Flow in Java
Java provides three types of control flow statements.
[Link] Making statements
[Link] statements
[Link] statement
[Link] statements
[Link] while loop
[Link] loop
[Link] loop
[Link]-each loop
[Link] statements
[Link] statement
[Link] statement
if statements
[Link] if statement
[Link]-else statement
[Link]-else-if ladder
[Link] if-statement