Java Basics

Gaurav Shah
8 min readNov 30, 2023

--

Introduction

Java is a widely used, high-level, object-oriented programming language designed to be platform-independent and secure. It was developed by Sun Microsystems (now owned by Oracle Corporation) and released in 1995. Java’s design principles focus on simplicity, robustness, portability, and performance. In this blog, we’ll delve into the key concepts, features, and it’s components.

Java Basics

Java source code file

Java Source Code File
A Java source code file defines Java entities such as a class, interface, enum, and annotation.
All your Java code should be defined in Java source code files.

Multiple classes in a Java Source code file
The classes and interfaces can be defined in any order of occurrence in a Java source code file.
When you define a public class or an interface in a Java source file, the names of the class or interface and the Java source file must match.
A source code file can’t define more than one public class or interface.

Structure of Java class

  1. Package Statement: A package statement explicitly defines which package a class is in.
    a. If a class includes a package statement, it must be the first statement in the class definition.
    b. The package statement must appear exactly once in a class.
  2. Import Statement: Classes and interfaces in the same package can use each other without prefixing their names with the package name.
    But to use a class or an interface from another package, you must use its fully qualified name, that is, PackageName.anySubpackageName.ClassName . For example, the fully qualified name of class String is java.lang.String. The import statement follows the package statement but precedes the class declaration.
  3. Comments: You can also add comments to your Java code. Comments can appear at multiple places in a class. A comment can appear before and after a package statement, before and after the class definition, as well as before and within and after a method definition.
  4. Javadoc Comments: Javadoc comments are special comments that start with /** and end with */ in a Java source file.
    These comments are processed by Javadoc, a JDK tool, to generate API documentation for your Java source code files.
  5. Class: A class is a design used to specify the attributes and behavior of an object. A class is a design from which an object can be created.
    The attributes of an object are implemented using variables, and the behavior is implemented using methods.

Components of a class

  1. Java Package: The Package is used to group a related set of classes and interfaces. Packages also provide access protection and namespace management. All classes and interfaces are defined in a package.
    If you don’t include an explicit package statement in a class or an interface, it’s part of a default package.
    Important rules about packages:
    ■ As per Java naming conventions, package names should all be in lowercase.
    ■ The package and subpackage names are separated using a dot.
    ■ Package names follow the rules defined for valid identifiers in Java.
    ■ For classes and interfaces defined in a package, the package statement is the first statement in a Java source file (a .java file). The exception is that comments can appear before or after a package statement.
    ■ There can be a maximum of one package statement per Java source code file (.java file).
    ■ All the classes and interfaces in a Java source code file are defined in the same package. They can’t be defined in separate packages.

2. Import Statements: The import statement enables us to use simple names instead of fully qualified names for classes and interfaces defined in separate packages.
The import statement doesn’t embed the contents of the imported class in your class, which means that importing more classes doesn’t increase the size of your class.
You can import an individual static member of a class or all its static members by using a static import statement.
■ It’s important to note that you can’t use the import statement to access multiple classes or interfaces with the same names from different packages.
■ You can’t import classes from a subpackage using an asterisk in the import statement.
■ By using the wildcard character, an asterisk (*), you can import all the public members, classes, and interfaces of a package.
■ Using an asterisk to import all package members has a drawback. It may be harder to figure out which imported class or interface comes from which package.
■ An attempt to use an import statement to import both the classes with the same name in the same class will not compile.
For example, the Java API defines class Date in two commonly used packages: java.util and java.sql.
To define variables of these classes in a class, use their fully qualified names with the variable declaration

Importing classes with the same name

3. Class: A class is a design from which an object can be created.

  1. Main Method: The first requirement in creating an executable Java application is to create a class with a method whose signature (name and method arguments) matches the main method.
    The main method should comply with the following rules:
    ■ The method must be marked as a public method.
    ■ The method must be marked as a static method.
    ■ The name of the method must be main.
    ■ The return type of this method must be void.
    ■ The method must accept a method argument of a String array or a variable argument (varargs) of type String.
    Note: The placement of the keywords public and static can be interchanged, Both public static and the static public are the valid order of keywords to declare the main method.
  2. Constructor: A class constructor is used to create and initialize the objects of a class.
    A class can define multiple constructors that accept different sets of method parameters.
  3. Variables: The instance variables are defined within a class but outside all methods in a class.
    A single copy of a class variable or static variable is shared by all the objects of a class.
    It isn’t compulsory to define all variables of a class before defining its methods.
  4. Methods: A class method or static method can be used to manipulate the static variables.

4. Interface: An interface specifies a contract for the classes to implement. An interface is a grouping of related methods and constants. An interface can also define static methods.

Class Declaration

The class declaration marks the start of a class. It can be as simple as the keyword class followed by the name of the class.
A class name starts with the keyword class. class (lowercase c) isn’t the same as Class (uppercase C). You can’t use the word Class (uppercase C) to define a class.

Components of a class declaration

Executable classes versus non-executable classes:
An executable Java class, when handed over to the JVM, starts its execution at a particular point in the class: the main method.
The JVM starts executing the code that’s defined in the main method.

Executable Application

Java Features

Java offers multiple advantages over other languages and platforms.

  1. Platform Independent: It’s also referred to as “write once, run anywhere” (WORA). Java code can be executed on multiple systems without recompilation. Java code is compiled into bytecode, to be executed by a virtual machine — the Java Virtual Machine (JVM). Bytecode generated by a Java compiler is supported by all platforms with a JVM.
  2. Object Orientation: Java emulates real-life object definition and behavior. All Java code is defined within classes, interfaces, or enums. You need to create their objects to use them.
  3. Abstraction: Java lets you abstract objects and include only the required properties and behavior in your code.
  4. Encapsulation: With Java classes, you can encapsulate the state and behavior of an object. The state or the fields of a class are protected from unwanted access and manipulation. You can control the level of access and modifications to your objects.
  5. Inheritance: Java enables its classes to inherit other classes and implement interfaces. The interfaces can inherit other interfaces. This saves you from redefining common code.
  6. Polymorphism: Java enables instances of its classes to exhibit multiple behaviors for the same method calls.
  7. Type Safety: In Java, you must declare a variable with its data type before using it. This means that you have compile-time checks that ensure you never assign to a variable a value of the wrong type.
  8. Automatic Memory Management: Java uses garbage collectors for automatic memory management. They reclaim memory from objects that are no longer in use. This frees developers from explicitly managing the memory themselves. It also prevents memory leaks.
  9. Multithreading and Concurrency: Java has supported multithreading and concurrency.
  10. Security: Java includes multiple built-in security features to control access to your resources and execution of your programs.

Points to Remember

  1. The method parameters that are passed to the main method are also called command-line parameters or command-line values. As the name implies, these values are passed to a method from the command line.
  2. You don’t need an explicit import statement to use members from java.lang package. Classes and interfaces in this package are automatically imported into all other Java classes, interfaces, or enums.
  3. Members of a named package can’t access classes and interfaces defined in the default package.
  4. static methods and variables can’t access the instance members of a class.
  5. You can access static variables and methods using a null reference.
  6. A class may define an instance variable before or after the definition of a method and still use it.
  7. If you explicitly import a class name, it has precedence over any wildcards present.
  8. To compile and execute the code, execute the following:
    Compile (javac className.java), Run (java className)

Oracle Certified Associate (OCA) certification overview:
Oracle Certified Associate: Java SE 8 Programmer

For more Java and Spring-Boot-related blogs, check out my profile:
https://medium.com/@gauravshah97

Reach me out at:
https://www.linkedin.com/in/gauravshah97/

--

--