More about platform independent
JDK
JRE
Compiler Time
Runtime :-
JVM Execution
"Source code that we write will be saved using extension .java"
javac Main.java
java Main
public class Test{
public static void main(String[] args){
System.out.println("Hello World");
}
}
Data types
Description
Example
int
stores numeric digit
int i = 26;
char
stores characters
char c = 'A';
float
stores floating point numbers
float f = 98.67f;
double
stores larger decimal numbers
double d = 45676.58975;
long
stores numeric digit which is not able to stored in int
long l = 12234354354365l;
boolean
It only stores true and false
boolean b = false;
In float and long we have used f an dl. it denotes that the number in the variable is float or long type. If we don not use this java consider float value as double and long value as int.
Comments are something which is written in source code but ignored by the compiler.
// This is a single line comment
/* This is a
Multi line comment
*/
We have Scanner class available in java.util package to take input
//syntax
import java.util.Scanner;
public class Main{
public static void main(String [] args){
Scanner input = new Scanner(System.in);
}
}
int input → nextInt() is a function used to take input of int.
Scanner input = new Scanner(System.in);
int rollno = input.nextInt();
float input → nextFloat() is a function used to take input of float.
Scanner input = new Scanner(System.in);
float marks = input.nextFLoat();
string input →
Two ways to take string input
// 1. using next() methods - It will take one word input till a space occurs
Scanner input = new Scanner(System.in);
String s1 = input.next();
// 2. using nextLine() methods - It will take all string input including spaces.
Scanner input = new Scanner(System.in);
String s2 = input.nextLine();
1: Using System.out.print(): This method prints the given content without moving to the next line.
int number = 42;
System.out.print("The answer is: ");
System.out.print(number);
Output ↓
The answer is: 42
2: Using System.out.println(): This method prints the given content and moves to the next line after printing.
String message = "Hello, World!";
System.out.println(message);
System.out.println("This is a new line.");
Output ↓
Hello, World!
This is a new line.
3: Using System.out.printf(): This method allows you to format and print text with placeholders for variables.
String name = "Alice";
int age = 30;
System.out.printf("My name is %s and I am %d years old.", name, age);
Output ↓
My name is Alice and I am 30 years old.
4: Using Concatenation: You can use the + operator to concatenate strings and variables for printing.
double price = 19.99;
System.out.println("The item costs $" + price);
Output ↓
The item costs $19.99
import java.util.Scanner;
public class Sum {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter first no. ");
int num1 = input.nextInt();
System.out.print("Enter second no. ");
int num2 = input.nextInt();
int sum = num1+num2;
System.out.println("The sum is ="+ sum);
}
}
When one type of data is addigned to another type of variable an automatic type conversion will take place under some condition
When we convert one type of data to another type is known as type casting.
Ex :- int num = (int) (67.3455f)
While evaluating expressions the intermediate value may exceed the operands and hence the expression value will be promoted.
There are some condition for type promotion :-
byte a = 40;
byte b = 50;
byte c = 100;
int d = (a*c)/c;
System.out.println(d);
Here when a*b occurred it became 2000 which is out of the range of byte (as byte max can be 256). So, here byte is automatically promoted to int type.
public class TypePromotion {
public static void main(String[] args) {
byte b =42;
byte c = 'a';
short s = 1024;
int i = 50000;
float f = 5.67f;
double d = 0.1234;
double result = (f*b)+(i/c)+(d*s);
System.out.println((f*b)+" "+(i/c)+" "+(d*s));
System.out.println(result);
}
}
If we want to store large value into small data type
Ex : byte b = 50;
b = (byte) (b*2); // type casting int to byte.