You are on page 1of 6

Class Notes by Dr B P Sharma | http://fb.com/bpsharma.in | http://bpsharma.

in | 880060025

Session 2: Section A, 3pm to 5:00 pm


Data Types in Java
1. byte 1 byte
2. short 2 bytes
3. int 4 bytes
4. long 8 bytes
5. float 4 bytes
6. double 8 bytes
7. char 2 bytes (Unicode)
8. boolean undefined. According to Java Black Book 2
bytes
Literals or constant values
The values that we use from our side for some assignment or
expression are called as literals.
int n=5;
double ar=3.14*n*n;
Types of Literals
1. Integrals
2. Floats
3. Characters
4. Strings
5. Booleans

Call 8800600025 WhatsApp 8800360849 Website: http://www.bpsharma.in


Class Notes by Dr B P Sharma | http://fb.com/bpsharma.in | http://bpsharma.in | 880060025

Integrals
All numbers without decimal are called integrals.
By default such numbers of int type. Use l or L with long as
suffix.
int num=6;
long num=6L;
Types of Integrals
1. Decimal – 0 to 9, default
2. Octal – 0 to 7. Start with 0
3. Hexa Decimal – 0 to 9, A-F. Starts with 0x or 0X
4. Binary – 0 and 1. Starts with 0b or 0B
Use underscore (_) as number separator
int num=45_67_234;
int x=0B1111_0011_0011;

Call 8800600025 WhatsApp 8800360849 Website: http://www.bpsharma.in


Class Notes by Dr B P Sharma | http://fb.com/bpsharma.in | http://bpsharma.in | 880060025

File Naming rule


A Java program can have one or more classes.
If a class is public then program name and class name must be
same.
Floating Literals
Default is double. Use f or F with float as suffix
double n=5.6;
float n=5.6; //error
float n=5.6F; //correct
Character Literals
Enclosed in single quotes
Every character has corresponding ASCII value
char ch=’A’;
or
char ch=65;
String Literals
Enclosed in double quotes. Managed by String class
String name=”Rakesh”;

Call 8800600025 WhatsApp 8800360849 Website: http://www.bpsharma.in


Class Notes by Dr B P Sharma | http://fb.com/bpsharma.in | http://bpsharma.in | 880060025

Boolean Literal
Can have true or false only
boolean rented=false;
Getting data input from user
Java provides two methods for data input from user
1. Using java.util.Scanner class
2. Using java.io.BufferedReader class
Scanner class
- Used to input any kind of data from user using built-in
methods
o String next()
o int nextInt()
o float nextFloat()
o double nextDouble()
- We need to create the instance of Scanner class before
calling its methods
Scanner s=new Scanner(System.in);
Example
Write a program to input name and age of a person and check
it to be valid voter.
import java.util.Scanner;

Call 8800600025 WhatsApp 8800360849 Website: http://www.bpsharma.in


Class Notes by Dr B P Sharma | http://fb.com/bpsharma.in | http://bpsharma.in | 880060025

public class Voter


{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
System.out.print("Name : ");
String name=s.next();
System.out.print("Age : ");
int age=s.nextInt();
if(age>=18)
System.out.printf("Dear %s you can vote\n",name);
else
System.out.printf("Dear %s you cannot vote\n",name);
}
}

Call 8800600025 WhatsApp 8800360849 Website: http://www.bpsharma.in


Class Notes by Dr B P Sharma | http://fb.com/bpsharma.in | http://bpsharma.in | 880060025

Assignment
 WAP to input three numbers and show the biggest one.
 WAP to input 2 numbers and print all prime numbers in
the range.
 WAP to input a number and reverse print it.

Call 8800600025 WhatsApp 8800360849 Website: http://www.bpsharma.in

You might also like