Hello World Program
Variable and Data Types
Ranges of Data Types
Typecasting
Comments
Constants in Java - final
Reading Input - Scanner
class Main {
public static void main(String args[]) {
System.out.println("Hello World");
}
}
A variable is a container which stores a value in a Java program. Each variable has a type associated with it which is defined at its declaration.
class Main {
public static void main(String args[]) {
int num = 100;
System.out.println(num);
}
}
Here num is a variable of integer type storing value 100.
Variable name: A label for a memory location
Value: The something that would be stored in a variable
Storage: A place where data can be stored
Declaration: Announcing a variable (usually) at the beginning of a program
Naming convention: A set of rules about the names of variables
Assignment: Giving (setting) a variable a value
// Valid variable names
int marks1 = 50;
String student_name = "Naruto";
int id = 2;
// Invalid variable names
int 1_person;
Data Types mean to identify the type of the data and associate operations that can be done on the data values. Data types define the value that a variable can take.
Data types also tell us information about:
Data Type | Range | Size |
---|---|---|
byte | [-128 : 127] | 8 bits |
short | [-32,768 : 32767] | 16 bits |
int | [-2,147,483,648 : 2,147,483,647] | 32 bits |
long | [-9,223,372,036,854,775,808 : 9,223,372,036,854,775,807] | 64 bits |
float | [1.40239846 x 10^-45 : 3.40282347 x 10^38] |
32 bits |
double | [4.9406564584124654 x 10^-324 : 1.7976931348623157 x 10^308] |
64 bits |
public class Main {
public static void main(String[] args) {
String str = "I love Java";
String str2 = "Programming is awesome";
}
}
Comments are program text used to explain the program logic. They are ignored by the compiler. Comments help to make our code more readable and maintainable. The compiler and interpreter ignore comments, so they do not affect the program's behaviour or performance.
class Main {
public static void main(String args[]) {
// This is a single line comment
/*
This is
a
multiline
comment
*/
}
}
Typecasting refers to changing the type of data from one type to other.
It is of two types:
class Main {
public static void main(String args[]) {
final int num = 100;
System.out.println(num);
// num = 200; Error !!
}
}
Here num is a variable of integer type storing value 100.
num
Java offers a variety of solutions for reading inputs.
The simplest and the easiest way of reading input is through Scanner class.
import java.util.Scanner;
public class scannerInput {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter an integer");
int a=sc.nextInt();
System.out.println("Enter a string");
String s=sc.nextLine();
System.out.println("String is "+ s);
}
}
Given the values of principle, rate and interest, compute the Simple Interest.
Sample Input
P = 100
R = 5
T = 2
Sample Output
10
Write a program to read two numbers and print the sum of their squares.
Input:
3 4
Output:
25