Java programing syntax
Java programing syntax: Java is a high-level, object-oriented programming language. Below are some basic elements of Java syntax with examples:
Comments
- Single-line comment:
// This is a single-line comment
- Multi-line comment:
/* This is a multi-line comment */
Class Declaration
public class MyClass {
// class body
}
Main Method
public class MyClass {
public static void main(String[] args) {
// code to be executed
}
}
Variables
- Declaration:
int myNumber;
- Initialization:
myNumber = 5;
- Combined:
int myNumber = 5;
Data Types
int myNumber = 5;
double myDouble = 5.99;
char myChar = 'D';
boolean myBool = true;
String myString = "Hello";
Operators
- Arithmetic Operators:
+
,-
,*
,/
,%
- Comparison Operators:
==
,!=
,>
,<
,>=
,<=
- Logical Operators:
&&
,||
,!
Control Flow Statements
If Statement
if (condition) {
// code to be executed if condition is true
}
If-Else Statement
if (condition) {
// code to be executed if condition is true
} else {
// code to be executed if condition is false
}
Switch Statement
int day = 4;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
default:
System.out.println("Weekend");
}
Loops
For Loop
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
While Loop
int i = 0;
while (i < 5) {
System.out.println(i);
i++;
}
Do-While Loop
int i = 0;
do {
System.out.println(i);
i++;
} while (i < 5);
Methods
Method Declaration
public static void myMethod() {
// code to be executed
}
Method with Parameters
public static void myMethod(String fname) {
System.out.println(fname + " Refsnes");
}
Method Return Types
public static int myMethod(int x) {
return 5 + x;
}
Arrays
int[] myNumbers = {1, 2, 3, 4};
System.out.println(myNumbers[0]); // Output: 1
Object-Oriented Programming
Class and Object
public class MyClass {
int x = 5;
public static void main(String[] args) {
MyClass myObj = new MyClass();
System.out.println(myObj.x);
}
}
Constructor
public class MyClass {
int x;
public MyClass() {
x = 5;
}
public static void main(String[] args) {
MyClass myObj = new MyClass();
System.out.println(myObj.x);
}
}
Encapsulation
public class Person {
private String name;
public String getName() {
return name;
}
public void setName(String newName) {
this.name = newName;
}
}
Inheritance
class Animal {
public void animalSound() {
System.out.println("The animal makes a sound");
}
}
class Dog extends Animal {
public void animalSound() {
System.out.println("The dog says: bow wow");
}
}
public class Main {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.animalSound();
}
}
Polymorphism
class Animal {
public void animalSound() {
System.out.println("The animal makes a sound");
}
}
class Dog extends Animal {
public void animalSound() {
System.out.println("The dog says: bow wow");
}
}
class Cat extends Animal {
public void animalSound() {
System.out.println("The cat says: meow");
}
}
public class Main {
public static void main(String[] args) {
Animal myAnimal = new Animal();
Animal myDog = new Dog();
Animal myCat = new Cat();
myAnimal.animalSound();
myDog.animalSound();
myCat.animalSound();
}
}
These examples cover the basics of Java syntax and object-oriented programming concepts. I hope you will like this article.