Java Coding Basics, Syntax, Terminologies

By | November 5, 2023

java coding basics

Java coding Basics: Java is an object-oriented programming language with many advanced features. The Java language is based on objects and classes. Java coding basics comprise learning Java basic syntax, keywords, classes, objects, semantics, etc. 

The Java codes are enclosed inside the class and consist of objects with different method calls. Java is a user-friendly language which consists of easy-to-learn syntax and semantics. Let us understand the basic terminologies in Java and Java coding basics.

Join our Java DSA Development courses at PW Skills. Anyone who wants to start with programming with data structure can join this course. The course contains practice exercises, assignments, and quizzes along with the PW Skill lab. 

What are the Java Coding Basics?

Java basics consist of learning Java basic syntax and terminologies used in Java language. We will learn to name variables in Java, keywords, case sensitivity, name extension, modifiers, documentation, comments, and other essential things to start with Java.

Also check: How to start learning Java

Java Coding Basic Terminologies

Java is an object-oriented programming language which consists of objects, classes, and methods inside programs. There are three main terminologies in Java coding basics: class, objects, and methods. 

  • Class: Classes in Java are the blueprint used to contain objects inside. It is used to define the object attributes and methods. For example, consider a class car with attributes like brand, year of manufacture, etc.
  • Objects: An object is an instance of a class based on a real-world entity. Objects are real-world entities that are created inside the class.
  • Methods: Methods are the function which consists of the operation defined inside a class. Methods are used to perform different operations and actions inside the class.  

Let us understand the terminologies with the help of an example. Consider a class Car with attributes of brand and year. We will also define a method named displayInfo () used to display the information of the car as output. 

Java Coding Basics Terminologies 
public class Car {

    // Attributes

    String brand;

    int year;

    // Constructor

    public Car(String brand, int year) {

        this.brand = brand;

        this.year = year;

    }  

    // Method to display information about the car

    public void displayInfo() {

        System.out.println(“Brand: ” + brand);

        System.out.println(“Year: ” + year);

    }

}

public class Main {

    public static void main(String[] args) {

        // Creating objects of the Car class

        Car car1 = new Car(“Toyota”, 2020);

        Car car2 = new Car(“Honda”, 2019);

        car1.displayInfo();

        car2.displayInfo();

    }

}

Output

Java Coding Basics
Brand: Toyota Year: 2020

Brand: Honda Year: 2019

Java Coding Basics Syntax

Let us discuss the basic syntax of Java here.

1. FileName 

The file name of a Java file is saved with the .java extension. We want to name our file NewFile. Then, we will use NewFile.java 

2. Case Sensitivity

In Java, lowercase and uppercase are considered distinct as Java is a case-sensitive language. The class name generally starts with uppercase, and other variables generally follow mixed case sensitivity, such as camelCase or starting with lowercase. 

Java Coding Basics
public class CaseSensitivityExample {

    public static void main(String[] args) {

        int number = 42;

        int Number = 24;

        // These are two different variables due to case sensitivity

        System.out.println (“number: ” + number);  // Output: 42

        System.out.println(“Number: ” + Number);  // Output: 24

        // Variable names are case-sensitive

        int numBer = 100;

        System.out.println(“numBer: ” + numBer);  // Output: 100

        // Class names are case-sensitive

        MyClass myObject = new MyClass();

        myObject.printMessage();  // Output: Hello, World!

     }

}

class MyClass {

    void printMessage() {

        System.out.println(“Hello, World!”);

    }

}

3. Comments in Java 

In Java, comments are added using the ‘//’ and ‘/*….*/’ tags. There are three types of comments in Java. 

  • Single Line comment: This consists of a short one-line comment. The  ‘//’ tags are used to enclose the single-line comments.
  • Multi-line comment: In Java, multiline comments are enclosed within ‘/*…*/’ tags. 
  • Documentation: The doc comments are enclosed within ‘/**…..*/’tags. These tags are used to enclose important documents within a program. 

4. Class Name

The class name in Java starts with a capital letter (uppercase). We should always start class names with an uppercase, not with numbers or any special character. 

Java Coding Basics ClassName
class MyClass 

class oneClass

class Car

class MyFirstJava

class My$Java

5. Public static void main (String[], args)

The public static void main () is the entry point inside the Java program, like used in other programs. However, Java highly uses OOPs concepts, hence, it contains public, static, void, main and the arguments. Let us know their uses. 

  • The public keyword is an access modifier used in the Java main program to allow external members to access the class from outside. 
  • The static keyword used in the main to represent the class main method belongs to the class and is not an instance of the class. 
  • The void is used to tell the return type of the main method.
  • The string[], args defines an array of strings which can be passed in the program. 

6. Method Name in Java

Methods in Java are used to perform some action or a specific task. It defines the behaviour of an object inside the class. It is important to follow the guidelines while naming Methods in Java.

  • The method name should be close to the action it performs, and it should describe the specific task it performs. 
  • The method name starts with a lowercase letter and can consist of the camelCase naming technique. 
  • If the length of the method name is long, then each different name inside the letter name should start with uppercase, such as myFunction(), myCar(), countFibonnaciNumber(), etc.
Java Coding Basics Method Names
Public void myFunction ()

public void myCar ()

public void countFibonnaciNumber ()

7. Identifiers in Java

Java identifiers are case sensitive and start with an underscore (‘_’) , or a lowercase alphabet. However, starting with numbers and special symbols are not allowed in Java. After the first character, we can use any number of symbols, numbers or alphabets. Check out some of the examples of valid identifiers in the table below.

 Java Coding Basics Identifiers
minNumber, factorialNo, _myNumber, $rupees, etc.

8. Access Modifiers 

Access Modifiers are used to determine the visibility of classes, methods, and variables in Java. There are three main types of access modifiers in Java.

  • Public: Classes declared with the Public keyword are accessible by every member, whether it is from the same class or another. 
Java coding Basics Public Access Specifiers 
Access within class Yes
Access within package Yes
Access outside package Yes
Access by subclass Yes

Example of Public Access specifiers

Java Coding Basics Public Specifiers
public class MyClass {

    public void myPublicMethod() {

        // Code here

    }

}

 

  • Private: With the use of private specifiers, elements inside the class are accessed only within the same class. They cannot be accessed outside the class. It is used to implement encapsulation and abstraction in Java.
Java coding Basics Private Access Specifiers 
Access within class Yes
Access within package No
Access outside package No
Access by subclass No

Example of Private access specifier

Java Coding Basics Private Specifiers
public class MyClass {

    private int privateVariable;

    private void myPrivateMethod() {

        // Code here

    }

}

  • Protected: Class members can only be accessed by the same class subclasses inside the same package. Members outside the subclass cannot access the elements inside the class. 
 Java coding Basics Protected Access Specifiers 
Access within class Yes
Access within package Yes
Access outside package Yes
Access by subclass No

Example of Protected access specifier

Java Coding Basics Protected Specifiers
class MyClass {

    int packagePrivateVariable;

    

    void myPackagePrivateMethod() {

        // Code here

    }

}

9. Java Coding Basics Keywords

Reserved keywords are the keywords having predefined actions. Let us check some of the important Java-reserved keywords in the table below.    

Java Coding Basics Keywords 
abstract assert boolean break
byte case catch char
class const continue default
do double else enum
extends final finally float
for goto if implements
import instanceof int interface
long native new package
private protected public return
short static strictfp super
switch synchronized this throw
throws transient try void
volatile while    
Recommended Reads
Java Vs C++ Vs Python Java or C++ which is better
What is STL in C++ Top Features of C++ programming language

Java Coding Basics FAQs

Q1. What are Java coding basics?

Ans: Java coding basics include having knowledge of basic syntax and terminologies in Java. It includes case sensitive, file name, class name, main, identifiers and keywords.

Q2. Can I start a class name with a lowercase letter?

Ans: No, class names generally start with an uppercase letter. Check the article for complete class name guidelines.

Q3. How many access specifiers are there in Java?

Ans: There are three access specifiers in Java: public, private, and protected. They are used to control the access of external members outside the class.

Telegram Group Join Now
WhatsApp Channel Join Now
YouTube Channel Subscribe
Scroll to Top
close
counselling
Want to Enrol in PW Skills Courses
Connect with our experts to get a free counselling & get all your doubt cleared.