Your path to becoming an Ethical Hacker! Hacking Academy Try It Now!

Java Basic Syntax

Explore Java programming basics: classes, objects, methods, syntax, and keywords. Learn Java fundamentals with practical examples.

Java Basic Syntax

Java is an object-oriented programming language, meaning it relies on the concept of objects, which interact through method calls to function together. This article covers fundamental concepts in Java, such as classes, objects, methods, instance variables, syntax, and semantics.

Basic Terminologies in Java

  1. Class: A class acts as a blueprint for creating objects. It is a logical template that defines common properties and methods.
    • Example 1: A blueprint of a house represents a class.
    • Example 2: In real life, Alice is an object of the “Human” class.
  2. Object: An object is an instance of a class, having its own state and behavior.
    • Example: Dogs, cats, and monkeys are objects of the “Animal” class.
    • Behavior: Running on the road.
  3. Method: A method defines the behavior of an object.
    • Example: A fuel gauge shows the amount of fuel left in a car.
  4. Instance Variables: These are unique to each object and define its state through the values assigned to them.

    Steps to compile and run a Java program in a console:

    javac LT.java
    java LT
    import java.util.*;
    public class LT {
        public static void main(String[] args) {
            System.out.println("LearnTricking!");
        }
    }
    Output:
    LearnTricking!

Note: If a class is public, the file name must match the class name.

Related Posts


Java Syntax Overview

Java syntax defines how Java code is written and understood by the Java compiler. Here are the essential elements of Java syntax:

1. Comments in Java

There are three types of comments in Java:

  • Single-line Comment: Begins with //.
    // System.out.println("This is a comment.");
  • Multi-line Comment: Enclosed between /* and */.
    /*
        System.out.println("This is the first line of the comment.");
        System.out.println("This is the second line of the comment.");
    */
  • Documentation Comment: Also known as a doc comment, starts with /** and ends with */.
    /** documentation */

2. Source File Name

The source file name must match the public class name and end with .java. If the file contains a public class named LT, the file name should be:

LT.java  // valid syntax
lt.java  // invalid syntax

3. Case Sensitivity

Java is case-sensitive, meaning identifiers like AB, Ab, aB, and ab are different.

System.out.println("LearnTricking"); // valid syntax
system.out.println("LearnTricking"); // invalid syntax due to incorrect capitalization of 'System'

4. Class Names

  • The first letter of a class name should be uppercase (though lowercase is allowed, it's discouraged).
  • For class names with multiple words, each inner word’s first letter should be uppercase.
class MyJavaProgram  // valid syntax
class 1Program       // invalid syntax
class My1Program     // valid syntax
class $Program       // valid but discouraged
class My$Program     // valid but discouraged (indicates inner class Program inside My)
class myJavaProgram  // valid but discouraged

5. public static void main(String[] args)

The main() method is the entry point of a Java program. An alternative signature is public static void main(String... args).


6. Method Names

  • Method names should start with a lowercase letter (though uppercase is allowed, it's discouraged).
  • For multi-word method names, each inner word's first letter should be uppercase.
public void employeeRecords() // valid syntax
public void EmployeeRecords() // valid but discouraged

7. Identifiers in Java

Identifiers are names for variables, classes, methods, etc., and must follow these rules:

  • Can start with a letter, currency symbol, or underscore (_). Conventionally, variables should start with a lowercase letter.
  • Can contain letters, digits, currency symbols, and underscores after the first character. Constants should be all uppercase.
  • Case-sensitive.
  • Keywords cannot be used as identifiers.
Legal identifiers: MinNumber, total, ak74, hello_world, $amount, _under_value
Illegal identifiers: 74ak, -amount

8. White Spaces in Java

Blank lines, which may contain only white spaces or comments, are ignored by the Java compiler.


9. Access Modifiers

Modifiers control the scope of classes and methods.

  • Access Modifiers: default, public, protected, private
  • Non-access Modifiers: final, abstract, static, transient, synchronized, volatile, native

10. Understanding Access Modifiers

Access Modifier Within Class Within Package Outside Package by Subclass Only Outside Package
Private Yes No No No
Default Yes Yes No No
Protected Yes Yes Yes No
Public Yes Yes Yes Yes

11. Java Keywords

Keywords are reserved words in Java used for specific purposes and cannot be used as identifiers.
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

Post a Comment

Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.