0% found this document useful (0 votes)
8 views22 pages

4 Var DT Opr KW (Unit 1)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views22 pages

4 Var DT Opr KW (Unit 1)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 22

CONTENTS

 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); }

public static void main(String[] args) {


VarEx obj = new VarEx();
System.out.println("Accessing Instance Variable using object: " + obj.instVar);
System.out.println("Accessing Static Variable using class name: " + VarEx.stVar);
obj.exampleMethod();
}
}
use of static variable
class stock{
public static int price= 100;
public st() {
price++;
System.out.println("Price for this share is=>>"+price);
}
}
public class stEx {
public static void main(String args[]) {
stock st1 = new stock();
System.out.println(st1.price);
stock st2 = new stock();
System.out.println(st2.price);
stock st3 = new stock();
System.out.println(st3.price);
} }
Constants in Java
• is a value that cannot be changed after assigning it.
• Defined by using the non-access modifiers static and final.
According to naming convention the identifier name must be in capital letters.
Example: static final double PRICE=432.78;

Example:
DATA TYPES
• Data types specify the different sizes and values that can be stored in the variable.

There are two types of data types in Java:

1.Primitive data types: It include boolean, char, byte, short, int, long, float & double.

2.Non-primitive data types: It include Classes, Interfaces, and Arrays.


Data Types, Size, Minimum and Maximum Range along with Default Value

Byte Data Type


•used to save memory in large arrays
•It is 4 times smaller than an integer.
Short Data Type
Also be used to save memory
2 times smaller than an integer.

Example: Boolean one = false


Example: byte a = 10, byte b = -20
Example: short s = 10000, short r = -5000
Example: int a = 100000, int b = -200000
Example: float f1 = 234.5f
Example: long a = 100000L, long b = -200000L
Example: double d1 = 12.3
Example: char letterA = 'A'
OPERATORS
An operator is a symbol which is used to perform operations. For example: +, -, *, / etc.

Types of operators:
Operator Type Category Precedence

• Unary Operator, Unary postfix expr++ expr--


• Arithmetic Operator, prefix ++expr --expr +expr -expr ~ !
• Shift Operator, Arithmetic multiplicative */%
• Relational Operator, additive +-
• Bitwise Operator, Shift shift << >>
• Logical Operator, Relational comparison < > <= >=
• Ternary Operator and
equality == !=
• Assignment Operator.
Bitwise bitwise AND &
bitwise exclusive OR ^
bitwise inclusive OR |
Logical logical AND &&
logical OR ||
Ternary ternary ?:
Assignment assignment = += -= *= /= %= &= ^= |= <<=
>>=
Left Shift and Right Shift Operator
<< is used to shift all of the bits in a value to the left side of a specified number of times.
class OperatorExample
{
Example
public static void main(String args[])
{
System.out.println(10<<2); //10*2^2=10*4=40
System.out.println(10<<3); //10*2^3=10*8=80
System.out.println(20<<2); //20*2^2=20*4=80
System.out.println(15<<4); //15*2^4=15*16=240
}}

>> is used to move left operands value to right by the no. of bits specified by the right operand.

Example class OperatorExample


{
public static void main(String args[]){
System.out.println(10>>2); //10/2^2=10/4=2
System.out.println(20>>2); //20/2^2=20/4=5
System.out.println(20>>3); //20/2^3=20/8=2
} }
Relational Operator Example:

Example and Output:

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

• Such operators are used to perform bit operations.


• All the decimal values will convert into binary values (sequence of bits i.e., 0100,
1100, 1000, 1001, etc.).
• The Java Bitwise Operators will work on these bits such as shifting them left to right
or converting bit value from 0 to 1 etc.
Bitwise &(AND) Operator Example:

Bitwise | (inclusive OR) Operator Example:


Bitwise ^(exclusive OR) Operator Example:

Example:

• Used as one liner replacement for if-then-else statement.


Ternary Operator
• It is the only conditional operator which takes three operands.

class TrnyOpr {
public static void main(String[] args) {

int febDays = 29;


String result;

result = (febDays == 28) ? "Not a leap year" : "Leap year";


System.out.println(result);
}
}
KEYWORDS
• Also known as reserved words.
• Keywords are particular words which acts as a key to a code.
• These are predefined words by Java so it cannot be used as a variable or object name.
Java Comments
• Statements that are not executed by the compiler and interpreter.
• Used to provide information or explanation about the variable, method, class or any statement
Types of Java Comments: 1. Single Line Comment (//) 2. Multi Line Comment(/* */)

Single Line Comment (//)

MultiLine Comment(/* */)


- used to comment multiple lines.
Example:

public class ComEx {


public static void main(String[] args)
{
int i=10; //Here, i is a variable
System.out.println(i);
/* Let's declare and
print variable in java. */
} }
Scanner and BufferedReader
• Two classes that have been used for reading files for a very long time.
Scanner import java.util.Scanner;
- java.util.Scanner class is a simple text
scanner which can parse primitive types class scannerEx {
and strings. public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
•A user can read numbers from System.in
by using Scanner class. System.out.println("Enter a String:");
String a = sc.nextLine();
•Methods used by Scanner class(nextXXX()) : System.out.println("Enter an integer:");
int b = sc.nextInt();
Such as: nextInt(), nextFloat(), nextByte(),
System.out.println("You have entered name as " + a);
nextShort(), nextDouble(), nextLong(),
next().
System.out.println("You have entered number as: " + b);
BufferedReader
• java.io.BufferedReader class reads text from a character-input stream, buffering
characters so as to provide for the efficient reading of sequence of characters
• It reads characters from any input stream line.
• It can use readLine() to read data line by line.
import java.io.*;
class Differ { Output:
public static void main(String args[]) throws IOException { Enter an integer
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter an integer"); 12
int a = Integer.parseInt(br.readLine());
System.out.println("Enter a String");
String b = br.readLine(); Enter a String
System.out.printf("You have entered:-\n " + a +
" and name as " + b); KIRAN
} }
Key differences between the Scanner and BufferedReader class :
Both BufferedReader and Scanner can read a file or user input from the command prompt, but
some significant differences between them are:

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

THE FUNCTIONS OF PARSE METHODS :


•ParseInt(); – It is a member function in Integer Class;
•ParseDouble(); – It is a member function in Double Class;
•ParseFloat(); – It is a member function in Float Class;
Example (With Parse ) :

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);
}
}

Compile : Javac A.java


Execution : Java A 10 20
Output : 11 21

You might also like