EZ

Eduzan

Learning Hub

Eduzan
Eduzan / Java

Basic Concepts of Java

Java is an object-oriented programming language, which means that it consists of objects that interact with each other through method calls to perform various tasks. Here’s an overview of some essential concepts in Java:

Basic Terminologies in Java:

1. Class:A class is a blueprint for objects. It defines a logical template that shares common properties and methods.

2. ObjectAn object is an instance of a class. It represents an entity with a behavior and state.

3. Method: A method defines the behavior of an object.

4. Instance Variables: Each object has its own unique set of instance variables. These variables represent the state of an object, defined by the values assigned to them.

Example: Compiling and Running a Java Program in a Console

import java.util.*;
public class Main {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

Output:

Hello, World!

5. Comments in Java: There are three types of comments in Java:

  • Single-line Comment:
// This is a single-line comment
  • Multi-line Comment:
/*
    This is a multi-line comment
*/
  • Documentation Comment:
/** This is a documentation comment */

6. Source File Name: The source file name should exactly match the public class name with the extension .java. If there is no public class, the file name can differ.

Example:

GFG.java  // valid
gfg.java  // invalid if public class is GFG

7.  Case Sensitivity:
Java is case-sensitive, meaning identifiers like ABAbaB, and ab are considered different.

Example:

System.out.println("Hello");  // valid
system.out.println("Hello");  // invalid

8. Class Names: The first letter of a class name should be uppercase.If multiple words are used, each word should start with an uppercase letter.

Example:

class MyClass      // valid
class 1Program     // invalid
class My1Program   // valid
class $Program     // valid but discouraged

9. Main Method: The main() method is the entry point of every Java program:

public static void main(String[] args) {
    // program logic
}

10. Method Names: Method names should start with a lowercase letter.If multiple words are used, the first letter of each word (after the first one) should be uppercase.
Example:

public void calculateSum() // valid

11. Identifiers in Java: Identifiers are names given to classes, methods, variables, etc. They must follow certain rules:Can begin with a letter, underscore (_), or currency symbol. Subsequent characters can be letters, digits, or underscores. Java keywords cannot be used as identifiers. Legal identifiers: myVar, hello_world, $amount.
Illegal identifiers: 1Var, -amount.

12. White Spaces in Java: Blank lines, spaces, and comments are ignored by the Java compiler.

13. Access Modifiers: Java provides access control to classes and methods through access modifiers:

Basic Concepts of Java
Access ModifierWithin ClassWithin PackageOutside Package by SubclassOutside Package
PrivateYesNoNoNo
DefaultYesYesNoNo
ProtectedYesYesYesNo
PublicYesYesYesYes

14. Java Keywords:
Keywords in Java have predefined meanings and cannot be used as identifiers. Examples include classinterfacevoidintpublicstatic, etc.

End of lesson.