Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

Syntax of Writing Java Code

The document discusses the key concepts in Java including class declarations, main methods, variables, data types, operators, control structures, methods, objects, inheritance, interfaces, exceptions, packages, import statements, and comments. It provides code examples for many of these concepts.

Uploaded by

lucyngei2021
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Syntax of Writing Java Code

The document discusses the key concepts in Java including class declarations, main methods, variables, data types, operators, control structures, methods, objects, inheritance, interfaces, exceptions, packages, import statements, and comments. It provides code examples for many of these concepts.

Uploaded by

lucyngei2021
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

Class Declaration:

A Java program starts with the declaration of a class. A class is a blueprint for creating objects. The class
declaration includes the class name, access modifier, and body.

java

Copy code

1public class MyClass {

2 // class body

3}

2. Main Method:

The main method is the entry point of a Java program. It must be declared with
the public and static modifiers, and it must return void.

java

Copy code

1public static void main(String[] args) {

2 // code to be executed

3}

3. Variables:

Variables are used to store data in a Java program. They can be declared using a data type followed by a
variable name.

java

Copy code

1int myNum; // declares an integer variable

2double myDouble = 5.5; // declares and initializes a double variable

4. Data Types:

Java has several data types, including:

 Primitive types: byte, short, int, long, float, double, char, boolean

 Reference types: String, Object, Array

5. Operators:

Java supports several operators, including:

 Arithmetic operators: +, -, *, /, %
 Relational operators: <, >, <=, >=, ==, !=

 Logical operators: &&, ||, !

 Bitwise operators: &, |, ^, ~, <<, >>, >>>

 Assignment operators: =, +=, -=, *=, /=, %=, <<=, >>=, &=, ^=, |=

6. Control Structures:

Java supports several control structures, including:

 Conditional statements: if, else, switch, case

 Loops: for, while, do-while

 Jump statements: break, continue, return

7. Methods:

Methods are used to perform specific tasks in a Java program. They can be declared with a name, return
type, and parameters.

java

Copy code

1public int add(int a, int b) {

2 return a + b;

3}

8. Objects and Classes:

Objects are instances of a class. They can be created using the new keyword.

java

Copy code

1MyClass myObject = new MyClass();

9. Inheritance:

Java supports inheritance, which allows a class to inherit the properties and methods of another class.

java

Copy code

1public class MyClass extends MySuperClass {

2 // class body

3}
10. Interfaces:

Interfaces are used to define a contract for a class. They can be implemented using
the implements keyword.

java

Copy code

1public class MyClass implements MyInterface {

2 // class body

3}

11. Exceptions:

Java supports exception handling, which allows a program to handle errors and exceptions.

java

Copy code

1try {

2 // code that might throw an exception

3} catch (Exception e) {

4 // code to handle the exception

5} finally {

6 // code to be executed whether an exception is thrown or not

7}

12. Packages:

Packages are used to organize related classes and interfaces into a single unit.

java

Copy code

1package mypackage;

13. Import Statements:

Import statements are used to include classes and interfaces from other packages.

java

Copy code

1import java.util.ArrayList;
14. Comments:

Comments are used to add notes and explanations to a Java program.

java

Copy code

1// single-line comment

2/* multi-line

3comment */

You might also like