Java
Java
It is used for:
A Java virtual machine (JVM) is a virtual machine that enables a computer to run
Java programs as well as programs written in other languages that are also
compiled to Java bytecode.
JVM Architecture
Let's understand the internal architecture of JVM. It contains classloader,
memory area, execution engine etc.
History of Java
James Gosling initiated Java language project in June 1991 for use in one of his many
set-top box projects. The language, initially called “Oak” after an oak tree that stood
outside Gosling's office, also went by the name ‘G‘Oak’Green’ and ended up later
being renamed as Java, from a list of random words.
Sun released the first public implementation as Java 1.0 in 1995. It promised Write
Once, Run Anywhere (WORA), providing no-cost run-times on popular platforms.
On 13 November, 2006, Sun released much of Java as free and open source software
under the terms of the General Public License (GPL).
Let us now briefly look into what do class, object, methods, and instance variables
mean.
Object − Objects have states and behaviors. Example: A dog has states - color,
name, breed as well as behavior such as wagging their tail, barking, eating. An
object is an instance of a class.
Class − A class can be defined as a template/blueprint that describes the
behavior/state that the object of its type supports.
Methods − A method is basically a behavior. A class can contain many
methods. It is in methods where the logics are written, data is manipulated and
all the actions are executed.
Sample Program
public class MyFirstJavaProgram
{
/* This is my first java program.
* This will print 'Hello World' as the output
*/
public static void main(String[] args) {
System.out.println("Hello World"); // prints Hello World
}
}
Java brings various Streams with its I/O package that helps the user to perform all
the input-output operations.
3 standard or default streams that Java has to provide which are also most
common in use:
1. System.in: This is the standard input stream that is used to read characters from
the keyboard or any other standard input device.
2. System.out: This is the standard output stream that is used to produce the
result of a program on an output device like the computer screen.
Here is a list of the various print functions that we use to output statements:
print(): This method in Java is used to display a text on the console. This text
is passed as the parameter to this method in the form of String. This method
prints the text on the console and the cursor remains at the end of the text at
the console.
Syntax:
System.out.print(parameter);
// Java code to illustrate print()
import java.io.*;
class Demo_print {
public static void main(String[] args)
{
// using print()
// all are printed in the
// same line
System.out.print("GfG! “);
System.out.print("GfG! ");
System.out.print("GfG! ");
}
}
println(): This method in Java is also used to display a text on the console. It prints
the text on the console and the cursor moves to the start of the next line at the
console. The next printing takes place from the next line.
Byte
Byte data type is an 8-bit signed two's complement integer
Minimum value is -128 (-2^7)
Maximum value is 127 (inclusive)(2^7 -1)
Default value is 0
short
Short data type is a 16-bit signed two's complement integer
Minimum value is -32,768 (-2^15)
Maximum value is 32,767 (inclusive) (2^15 -1)
int
Int data type is a 32-bit signed two's complement integer.
Minimum value is - 2,147,483,648 (-2^31)
Maximum value is 2,147,483,647(inclusive) (2^31 -1)
long
Long data type is a 64-bit signed two's complement integer
Minimum value is -9,223,372,036,854,775,808(-2^63)
Maximum value is 9,223,372,036,854,775,807 (inclusive)(2^63 -1)
This type is used when a wider range than int is needed
Default value is 0L
float
Float data type is a single-precision 32-bit
Float is mainly used to save memory in large arrays of floating point numbers
Default value is 0.0f
System.out.println(myNum);
System.out.println(myFloatNum);
System.out.println(myLetter);
System.out.println(myBool);
System.out.println(myText);
Type casting
Convert a value from one data type to another data type is known as type casting.
byte -> short -> char -> int -> long -> float -> double
Exmple
public class WideningTypeCastingExample
{
public static void main(String[] args)
{
int x = 7;
//automatically converts the integer type into long type
long y = x;
//automatically converts the long type into float type
float z = y;
System.out.println("Before conversion, int value "+x);
System.out.println("After conversion, long value "+y);
System.out.println("After conversion, float value "+z);
}
}
double -> float -> long -> int -> char -> short -> byte
Example
public class NarrowingTypeCastingExample
{
public static void main(String args[])
{
double d = 166.66;
//converting double data type into long data type
long l = (long)d;
//converting long data type into int data type
int i = (int)l;
System.out.println("Before conversion: "+d);
//fractional part lost
System.out.println("After conversion into long type: "+l);
//fractional part lost
System.out.println("After conversion into int type: "+i);
}
}
Java Variables
To create a variable, you must specify the type and assign it a value:
Syntax
type variableName = value;
Example
public class Main {
public static void main(String[] args) {
String name = "John";
System.out.println(name);
}
}
Example
public class Main {
public static void main(String[] args) {
int myNum = 15;
System.out.println(myNum);
}
}
The Arithmetic Operators
Arithmetic operators are used in mathematical expressions in the same way that they
are used in algebra. The following table lists the arithmetic operators −
Assume integer variable A holds 10 and variable B holds 20, then −
Show Examples