Java Basics: An Introductory Overview

How is Java code executed?
A .java file (which is human-readable) goes through the compiler, which converts the entire file into a .class file (bytecode). This bytecode is then passed to the interpreter, which reads and executes it line by line, eventually converting it into machine code (0s and 1s).
What is a byte code?
This code will not run directly on a system.
We need the JVM (Java Virtual Machine) to run this.
This is why Java is platform-independent.
JDK vs JRE vs JVM vs JIT
JDK (Java Development Kit) = JRE (Java Runtime Environment) + Development Tools.
JRE (Java Runtime Environment) = JVM (Java Virtual Machine) + Library Classes.
JVM (Java Virtual Machine) = JIT (Just In Time) compiler + much more.
Important Points
Whatever we write will be in classes.
Every file with a
.javaextension is a class file.In programming languages, if a variable starts with a capital letter, it is considered a class (by convention).
If the file name is
Main.java, then theMainclass will be the public class.Public means the class is accessible from other files, packages, and literally from anywhere.
Class is the name of groups of properties and functions.
mainis a reserved keyword. It is the entry point of a Java program.staticis used because we need to run the main function without creating an object of the class.voidis the return type of the function, meaning it doesn't return any value.String[] argsrepresents the command-line arguments passed to the program.Q: What is a package?
A: It is the folder where your Java file resides.System.out.println()can be broken down as follows:Systemis a class that contains the variableout.outis a variable that refers to an instance ofPrintStream, which has the methodprintln.
import java.util.Scanner;is used for input purposes.Scanner input = new Scanner(System.in);can be broken down as follows:Scanneris a class that specifies the input stream.System.inmeans the input is taken from the keyboard.
Java follows the Unicode standard.
A data type that cannot be broken down further is called a primitive data type.
String is not a primitive data type; it is a reference type.
int a = 23,000; //not accepted int a = 23_000; //accepted
Type Conversion and Type Casting
Automatic type conversion will occur when one type of data is assigned to another type of variable, but only if the following conditions are met:
The two data types should be compatible.
The destination type should be larger than the source type. This type of conversion is known as Widening conversion.
(Narrowing conversion is when a larger data type is assigned to a smaller one, which requires explicit casting.)
int num = (int)(67.15f); //type casting (The programmer manually converts a higher data type into a lower data type.)
int a = 257; //automatic type promotion in expressions
byte b = (byte)(a); //257 % 256 = 1 (256 is the maximum value a byte can store.)
Rules for type promotion
- All
byte,short, andcharvalues are promoted toint. If any operand is of typelong, the entire expression is promoted tolong. If it is afloat, the whole expression is promoted tofloat, and so on.
This is NOT a PROFESSIONAL blog.



