Java Basics: Java is a high level programming language widely popular around the world. It supports object oriented programming, is platform independent and can easily be integrated with a large number of projects.
Java was developed by Sun Microsystem in 1991. Earlier the Sun Microsystem was known as OAK. Later Sun Microsystem took over the Oracle Corporation in 2009. In this article, let us know a few of the major java basics.
Java Editions
The Java Programming language developed in each addition with many new features and abilities integrated at each step. There are three major editions of Java.
Java Standard Editions (JSE)
This Java programming language creates programs for local desktop computers.
Java Enterprise Edition (JEE)
This edition allows java programs to run on large servers online. It can manage heavy loads and traffic.
Java Micro Edition (JME)
Java Micro Edition is a popular edition which supports small devices such as smartphones, appliances, etc. It is used to develop applications for small devices.
Also, check History of Java Programming Language
Java Basics for Beginners
Every Java program consists of a class, method, and other elements. Let us start from the Java basics structure.
public class HelloWorld {
public static void main(String[] args) { System.out.println(“Hello, World!”); } } |
- Public class HelloWorld: This is a class containing a java program inside. Every java program must contain at least one class.
- Public Static Void (String[] argos): It represents the main method in java and acts as an entry point in any Java application.
- System.out.println (): It is used to display the output over the console.
Also, check Hello World Program in Java
Why is Java Platform Independent?
Java platform is used to run a Java program. However, Java is a platform independent programming language. Java is platform-independent as it is compiled to bytecode when about to run on any device which has a Java Virtual Machine (JVM).
Source Code -> Compile -> Bytecode -> JVM -> Native Language (Linux, Windows, macOS)
JVM helps make Java programming language support various cross platform devices.
Also, Check Difference between Java Virtual Machine (JVM) and Just in Time (JIT)
Features of Java Basics Program
Java Basics language provides many features with a wide range of tools and framework available. Let us know some of the major features of the Java Programming language.
- Object-Oriented: Java is based on objects and classes, promoting reusable and modular code.
- Platform-Independent: Java programs run on any platform using the JVM (Write Once, Run Anywhere).
- Simple: Java’s syntax is clean and easy to understand, derived from C++ without complex features.
- Secure: Java provides security features like bytecode verification, access control, and encryption.
- Portable: Java bytecode can be executed on any system with a compatible JVM, making it portable.
- Multithreaded: Java supports multithreading, allowing concurrent execution of tasks for better performance.
- High Performance: With the use of Just-In-Time (JIT) compiler, Java programs offer high performance.
- Robust: Java provides strong memory management, exception handling, and type-checking mechanisms.
- Dynamic: Java supports dynamic loading of classes, making it adaptable and flexible during runtime.
- Distributed: Java has built-in support for networking, making it ideal for distributed computing.
- Interpreted: Java code is compiled to bytecode, which is interpreted by the JVM during execution.
Recommended Course
- Generative AI Course
- Python DSA Course
- Devops Course
- UI UX Course
- Digital Marketing Course
- Product Management Course
Java Basics: Object Oriented Programming Language
The Java Programming language is an object oriented programming language (OOPs) that involves solving problems by breaking them into small sub problems. In object-oriented programming there are two major elements.
- Objects
- Classes
Objects
An object is an instance of a class. It represents a real world entity that can be uniquely identified. For example, a chair, desk, door, etc can be considered as objects. An object consists of unique behaviour, state, and identity.
public class Main {
public static void main(String[] args) { //Creating an object of the Person Class Person person1 = new Person(“Alice”, 30); Person person2 = new Person(“Bob”, 25); person1.introduce(); person2.introduce(); } } |
Class
A class is a blueprint used for creating objects. It represents the methods and data types that will be used in the class. It is reusable and can be used to create multiple objects.
public class Person {
//Attributes
private String name; private int age; //Constructor
public Person(String name, int age) { this.name = name; this.age = age; } //Methods public void introduce() { System.out.println(“Hi, my name is ” + name + ” and I am ” + age + ” years old.”); } } |
Java Basics Interview Questions
Java is a popular programming language and hence the relevant job profiles are quite in demand in many top tech companies. Check some of the frequent Java basics interview questions below.
1. What is Java?
Java is a high-level, object-oriented programming language known for its platform independence (Write Once, Run Anywhere), meaning Java code can run on any device with a JVM (Java Virtual Machine).
2. What is JVM, JRE, and JDK?
- JVM (Java Virtual Machine): Executes Java bytecode and enables platform independence.
- JRE (Java Runtime Environment): Provides libraries and JVM to run Java applications.
- JDK (Java Development Kit): Contains tools for developing Java programs, including the JRE and compilers.
3. What are the main features of Java?
- Object-Oriented: Based on objects and classes.
- Platform-Independent: Java bytecode runs on any system with a JVM.
- Simple and Secure: Easy syntax and security features like memory management.
- Multithreaded: Supports concurrent execution of code.
- Robust: Strong error-checking and runtime exception handling.
4. What is the difference between an object and a class?
- Class: A blueprint for creating objects. It defines properties and methods.
- Object: An instance of a class with actual values for the properties.
5. What are the access modifiers in Java Basics?
- public: Accessible from anywhere.
- private: Accessible only within the same class.
- protected: Accessible within the same package and subclasses.
- default (no modifier): Accessible within the same package.
6. What is inheritance in Java?
Inheritance is a feature where one class (subclass) inherits the properties and behaviors (methods) of another class (superclass), promoting code reuse.
7. What is method overloading?
Method overloading allows multiple methods with the same name but different parameter lists (type or number) within the same class.
8. What is method overriding?
Method overriding occurs when a subclass provides a specific implementation of a method that is already defined in its superclass.
9. What is a constructor?
A constructor is a special method used to initialize objects when they are created. It has the same name as the class and no return type.
10. What is the difference between == and equals()?
- ==: Compares object references (whether they point to the same memory location).
- equals(): Compares the actual content/values of the objects (used for string or value comparison).
11. What is the final keyword in Java?
- final class: Cannot be subclassed (inherited).
- final method: Cannot be overridden by subclasses.
- final variable: Its value cannot be changed once assigned.
Also, check Final Keyword in Java
12. What is an interface in Java?
An interface is a reference type that can contain only abstract methods (until Java 8). Classes implement interfaces to provide method implementations. A class can implement multiple interfaces.
13. What is the difference between an interface and an abstract class?
- Abstract class: Can have both abstract and concrete (non-abstract) methods. A class can only inherit from one abstract class.
- Interface: Can have only abstract methods (before Java 8) and no instance variables. A class can implement multiple interfaces.
14. What are packages in Java?
A package is a namespace that organizes classes and interfaces into a single directory. It helps avoid naming conflicts and improves code maintainability.
15. What is exception handling in Java?
Exception handling manages runtime errors using try, catch, finally, throw, and throws blocks to prevent program crashes.
16. What is the difference between throw and throws?
- throw: Used to explicitly throw an exception from a method.
- throws: Declares that a method may throw one or more exceptions.
17. What are the different types of exceptions in Java?
- Checked Exceptions: Checked at compile time (e.g., IOException).
- Unchecked Exceptions: Occur at runtime (e.g., NullPointerException).
- Error: Serious issues not typically handled by Java code (e.g., OutOfMemoryError).
18. What is the static keyword in Java?
- Static variable: Shared across all instances of a class (class-level variable).
- Static method: Can be called without creating an instance of the class.
- Static block: Used to initialize static variables.
19. What is a string in Java?
A String is an immutable sequence of characters. Once created, a string cannot be changed. It is stored in a special memory area called the “string pool.”
20. What is the difference between StringBuilder and StringBuffer?
- StringBuilder: Mutable and not synchronized (faster, but not thread-safe).
- StringBuffer: Mutable and synchronized (thread-safe, but slower).
21. What is garbage collection in Java basics?
Garbage collection is an automatic memory management feature in Java. The JVM removes objects that are no longer being used to free memory.
22. What are the main types of loops in Java?
- for loop: Repeats a block of code a fixed number of times.
- while loop: Repeats while a condition is true.
- do-while loop: Executes at least once, then repeats as long as the condition is true.
23. What is multithreading in Java?
Multithreading allows multiple threads (independent paths of execution) to run concurrently within a program, improving performance for tasks like parallel processing.
24. What is the difference between break and continue?
- break: Exits the loop completely.
- continue: Skips the current iteration and moves to the next iteration of the loop.
25. What is the difference between stack and heap memory?
- Stack memory: Stores local variables and method calls; it is fast and automatically managed.
- Heap memory: Stores objects and is managed by the garbage collector.
Also, check Java Interview Questions for Beginners and Working Professionals
Learn Java Basics with PW Skills
Enrol in our Decode Java with DSA Course and master Java programming language along with Data Structures and Algorithms in Java. Learn how to build logic, pseudocode, and structure of a real world problem.
Work on real world projects and practices exercises to master Java language under the guidance of experienced mentors and interactive classes only at pwskills.com
Java Basics FAQs
Q1. What is this keyword in Java?
Ans: “this” keyword in Java is used to refer to the current instance of an object which can be used to access class members and invoke constructors.
Q2. What are Java Basics?
Ans: Java basics include Syntax, keyword, features, and different frameworks available in Java. Java is a high level general purpose object oriented programming language which is platform independent and can easily be integrated with multiple devices.
Q3. What is the Wrapper class in Java Basics?
Ans: A Wrapper class in Java basics is a way to use the primitive data types such as int, char, etc as objects.