4 Var DT Opr KW (Unit 1)
4 Var DT Opr KW (Unit 1)
VARIABLES
DATA TYPES
OPERATORS
KEYWORDS
VARIABLE
• A variable is a container which holds the value.
• It is name of reserved area allocated in memory.
• It is a combination of "vary + able" i.e. its value can be changed.
• It is assigned with a data type.
• There are 3 types of variables in java: local, instance and static.
Example to understand the types of variables
Types of Variables
class A{
a) Local variables: int data=50; //instance variable
static int m=100; //static variable
•Tied to method
void method(){
•Its scope is within the method. int n=90; //local variable
•It cannot be defined with "static" keyword. }
} //end of class
b) Instance variable:
•variable declared inside the class but outside the body of the method.
•It is not declared as static.
•Its value is instance specific and is not shared among instances, ie. Scope is whole class
c) Static variable:
•Variable declared is as static.
•Cannot be local.
•Its single copy is created and shared among all the instances of the class.
•Its memory allocation happens only once when class is loaded in memory.
Example: (Static variable , Instance variable, local variable)
public class VarEx {
static int stVar = 10;
int instVar = 20;
public void exMethod() {
int locVar = 30;
System.out.println("Static Variable: " + stVar);
System.out.println("Instance Variable: " + instVar);
System.out.println("Local Variable: " + locVar); }
Example:
DATA TYPES
• Data types specify the different sizes and values that can be stored in the variable.
1.Primitive data types: It include boolean, char, byte, short, int, long, float & double.
Types of operators:
Operator Type Category Precedence
>> is used to move left operands value to right by the no. of bits specified by the right operand.
class RelaOpr {
public static void main(String[] args) {
int a = 7, b = 11;
System.out.println("a is " + a + " and b is " + b);
System.out.println(a == b); // false
System.out.println(a != b); // true
System.out.println(a > b); // false
System.out.println(a < b); // true
System.out.println(a >= b); // false
System.out.println(a <= b); // true
}
Bitwise Operator
Example:
class TrnyOpr {
public static void main(String[] args) {
1. A scanner is a much more powerful utility than BufferedReader. It can parse the user input and
read an int, short, byte, float, long and double apart from String. On the other
hand, BufferedReader can only read String in Java.
2. BuffredReader has a significantly large buffer (8KB) than Scanner (1KB), which means if
you are reading long String from a file, you should use BufferedReader but for short input and
input other than String, you can use Scanner class.
3. BufferedReader is older than Scanner. It's present in Java from JDK 1.1 onward but
Scanner is only introduced in JDK 1.5 release.
4. BufferedReader is synchronized while Scanner is not. This means, you cannot share
Scanner between multiple threads but you can share the BufferedReader object.
Command-line Arguments
• CLA is the information that is passed to the program when it is executed.
• There is no restriction on the number of java command line arguments.
• Information is passed as Strings. They are captured into the String args of the main method.
• Information directly follows the program’s name on the command line when it is running.
Example:
While running a class Demo, you can specify command line arguments as
java Demo arg1 arg2 arg3 …
class Demo{
public static void main(String b[]){
System.out.println("Argument one = "+b[0]);
System.out.println("Argument two = "+b[1]);
}
}
# PARSE #
PARSE : It is a method which take a string(input) as an argument and convert in other formats as
like :
Integer
Float
Double
class A
{
public static void main(String args[])
{
int a=Integer.parseInt(args[0]);
int b=Integer.parseInt(args[1]);
System.out.println(a+1);
System.out.println(b+1);
}
}