Java is a statically-typed, object-oriented programming language known for its platform independence. This means that Java programs can be developed and compiled on one system, such as Windows, and run on another, like MacOS, without requiring any alterations to the source code.
Java is case-sensitive, meaning that uppercase and lowercase letters are treated as distinct. For example, variable
and Variable
are considered different variables.
public
keyword is used to specify the visibility of the class.class
keyword is followed by the class name, and the class body is enclosed in curly braces {}
.
public class MyClass {
// Class members go here
}
main
method, which serves as the entry point of the program.main
method is defined as public static void main(String[] args)
.
public static void main(String[] args) {
// Program logic goes here
}
//
, and multi-line comments are enclosed within /* */
.
// This is a single-line comment
/*
This is a
multi-line comment
*/
Java has several primitive data types (e.g., int
, long, double
, boolean,short,float,char,byte
) and reference data types (e.g., String
, user-defined classes).
int age = 29;
double pi = 3.14;
String name = "Reet";
Java supports various operators for performing operations like arithmetic, comparison, and logical operations.
Java provides conditional statements (if
, else
, switch
) and loops (for
, while
, do-while
) for controlling program flow.
new
keyword and access their members using the dot (.
) operator.
MyClass obj = new MyClass();
obj.method();
extends
keyword is used to establish inheritance.
class ChildClass extends ParentClass {
// Additional members and methods
}
try
, catch
, and finally
blocks to handle exceptions.
try {
// Code that may throw an exception
} catch (ExceptionType e) {
// Handle the exception
} finally {
// Code that runs regardless of whether an exception occurred
}
import
statement to access classes from other packages.
import java.util.ArrayList;
Java uses access modifiers like public
, private
, protected
, and default (no modifier) to control the visibility and accessibility of class members.
Methods are defined within classes and can take parameters and return values. Rules for Naming: Methods should be verbs, in mixed case with the first letter lowercase, with the first letter of each internal word capitalized.
public int add(int num1, int num2) {
return num1 + num2;
}
Java statements are terminated with semicolons (;
).
int x = 50; // Semicolon terminates the statement
Now that we have gained an understanding of data types, variables, and fundamental operators, let’s explore how we can integrate these components to create a basic, functional program. In Java, a program’s fundamental building block is a Class. A Class can encompass multiple fields (also known as properties), methods, and may even include inner classes as part of its structure. To make a Class executable, it must contain a main method. The main method serves as the starting point for the program’s execution.
Let’s construct a straightforward executable Class to demonstrate one of the code examples we discussed previously:
public class Person {
// Properties or fields
private String name;
private int age;
// Constructor
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// Method to get the person's name
public String getName() {
return name;
}
// Method to set the person's name
public void setName(String name) {
this.name = name;
}
// Method to get the person's age
public int getAge() {
return age;
}
// Method to set the person's age
public void setAge(int age) {
if (age >= 0) {
this.age = age;
} else {
System.out.println("Age cannot be negative.");
}
}
// Method to print information about the person
public void displayInfo() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
public static void main(String[] args) {
// Creating an instance of the Person class
Person person1 = new Person("Reet", 30);
// Accessing properties and methods of the person1 object
person1.displayInfo();
// Changing the person's age
person1.setAge(35);
// Displaying updated information
person1.displayInfo();
}
}
In this example, we have a Person
class with properties name
and age
, along with getter and setter methods to access and modify these properties. The displayInfo
method is used to print information about the person. In the main
method, we create an instance of the Person
class and demonstrate how to use its properties and methods.
These are some of the fundamental aspects of Java’s syntax. As you delve deeper into Java programming, you’ll encounter more advanced concepts and features that build upon this basic syntax.