Skip to main content

Command Palette

Search for a command to run...

Java Basics: An Introductory Overview

Updated
3 min readView as Markdown
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

  1. Whatever we write will be in classes.

  2. Every file with a .java extension is a class file.

  3. In programming languages, if a variable starts with a capital letter, it is considered a class (by convention).

  4. If the file name is Main.java, then the Main class will be the public class.

  5. Public means the class is accessible from other files, packages, and literally from anywhere.

  6. Class is the name of groups of properties and functions.

  7. main is a reserved keyword. It is the entry point of a Java program.

  8. static is used because we need to run the main function without creating an object of the class.

  9. void is the return type of the function, meaning it doesn't return any value.

  10. String[] args represents the command-line arguments passed to the program.

  11. Q: What is a package?
    A: It is the folder where your Java file resides.

  12. System.out.println() can be broken down as follows:

    • System is a class that contains the variable out.

    • out is a variable that refers to an instance of PrintStream, which has the method println.

  13. import java.util.Scanner; is used for input purposes.

  14. Scanner input = new Scanner(System.in); can be broken down as follows:

    • Scanner is a class that specifies the input stream.

    • System.in means the input is taken from the keyboard.

  15. Java follows the Unicode standard.

  16. A data type that cannot be broken down further is called a primitive data type.

  17. String is not a primitive data type; it is a reference type.

  18. 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:

  1. The two data types should be compatible.

  2. 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, and char values are promoted to int. If any operand is of type long, the entire expression is promoted to long. If it is a float, the whole expression is promoted to float, and so on.

This is NOT a PROFESSIONAL blog.