0% found this document useful (0 votes)
2 views

2-Elements-of-Java

Java is an object-oriented programming language where everything is associated with classes and objects. The main method serves as the entry point for Java applications, and identifiers must follow specific rules for naming. Key components of the main method include access modifiers, static keywords, and parameters for command-line arguments.

Uploaded by

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

2-Elements-of-Java

Java is an object-oriented programming language where everything is associated with classes and objects. The main method serves as the entry point for Java applications, and identifiers must follow specific rules for naming. Key components of the main method include access modifiers, static keywords, and parameters for command-line arguments.

Uploaded by

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

Java Classes/Objects

• Java is an object-oriented programming language.


• Everything in Java is associated with classes and
objects, along with its attributes and methods.
For example: in real life, a car is an object. The car
has attributes, such as weight and color,
and methods, such as drive and brake.
• A Class is like an object constructor, or a
"blueprint" for creating objects.
Create a Class
• To create a class, use the keyword class:
Main.java
Create a class named "Main" with a variable x:

A class should always start with an uppercase first


letter, and that the name of the java file should match
the class name.
• The main method in Java is the entry point for
any Java application. It is where the program
begins execution.
• In this example, the main method prints
"Hello, World!" to the console
Key Components of the Main Method
• public: This is an access modifier that allows the JVM
to access the main method from outside the class.
• static: This keyword allows the JVM to call
the main method without creating an instance of the
class.
• void: This indicates that the main method does not
return any value.
• main: This is the name of the method that JVM looks
for as the starting point of a Java program.
• String[] args: This parameter is an array of strings that
stores command-line arguments passed to the
program.
Different Ways to Write Main Method
• In Java, identifiers are the names given to different
elements such as variables, methods, classes, and labels.
These names are used to identify the elements within the
code, making it possible to refer to them in other parts of
the program or in subsequent operations.
Defining Java Identifiers
When creating identifiers in Java, there are specific rules that must be
followed to ensure that the names are valid and won't cause any
compile-time errors. Here are the key rules:

• Identifiers can include alphanumeric characters (A-Z, a-z, 0-9), the


dollar sign ($), and the underscore (_).
• The first character of an identifier must be a letter (A-Z or a-z), a
dollar sign ($), or an underscore (_). It cannot start with a digit.
• Case Sensitivity: Java identifiers are case-sensitive, meaning
"myVar" and "myvar" would be considered different identifiers.
• Length: There is no length limit for identifiers, but it's generally
recommended to keep them within a reasonable length for
readability, typically 4 to 15 characters.
• Reserved Words: Java has a set of reserved words that cannot be
used as identifiers because they serve specific purposes in the
language's syntax.
• Java main() Method
• The main() method is the entry point for any Java
application. It is where the program starts
execution.

You might also like