Java is an object-oriented programming language, meaning it relies on the concept of objects, which interact through method calls to function together. This article covers fundamental concepts in Java, such as classes, objects, methods, instance variables, syntax, and semantics.
Basic Terminologies in Java
-
Class: A class acts as a blueprint for creating objects. It is a
logical template that defines common properties and methods.
- Example 1: A blueprint of a house represents a class.
- Example 2: In real life, Alice is an object of the “Human” class.
-
Object: An object is an instance of a class, having its own state and
behavior.
- Example: Dogs, cats, and monkeys are objects of the “Animal” class.
- Behavior: Running on the road.
-
Method: A method defines the behavior of an object.
- Example: A fuel gauge shows the amount of fuel left in a car.
-
Instance Variables: These are unique to each object and define its
state through the values assigned to them.
Steps to compile and run a Java program in a console:
javac LT.java java LT
import java.util.*; public class LT { public static void main(String[] args) { System.out.println("LearnTricking!"); } }
LearnTricking!
Note: If a class is public, the file name must match the class name.
Related Posts
Java Syntax Overview
Java syntax defines how Java code is written and understood by the Java compiler. Here are the essential elements of Java syntax:
1. Comments in Java
There are three types of comments in Java:
-
Single-line Comment: Begins with
//
.// System.out.println("This is a comment.");
-
Multi-line Comment: Enclosed between
/*
and*/
./* System.out.println("This is the first line of the comment."); System.out.println("This is the second line of the comment."); */
-
Documentation Comment: Also known as a doc comment, starts with
/**
and ends with*/
./** documentation */
2. Source File Name
The source file name must match the public class name and end with
.java
. If the file contains a public class named
LT
, the file name should be:
LT.java // valid syntax lt.java // invalid syntax
3. Case Sensitivity
Java is case-sensitive, meaning identifiers like AB
,
Ab
, aB
, and ab
are different.
System.out.println("LearnTricking"); // valid syntax system.out.println("LearnTricking"); // invalid syntax due to incorrect capitalization of 'System'
4. Class Names
- The first letter of a class name should be uppercase (though lowercase is allowed, it's discouraged).
- For class names with multiple words, each inner word’s first letter should be uppercase.
class MyJavaProgram // valid syntax class 1Program // invalid syntax class My1Program // valid syntax class $Program // valid but discouraged class My$Program // valid but discouraged (indicates inner class Program inside My) class myJavaProgram // valid but discouraged
5. public static void main(String[] args)
The main()
method is the entry point of a Java program. An
alternative signature is public static void main(String... args)
.
6. Method Names
- Method names should start with a lowercase letter (though uppercase is allowed, it's discouraged).
- For multi-word method names, each inner word's first letter should be uppercase.
public void employeeRecords() // valid syntax public void EmployeeRecords() // valid but discouraged
7. Identifiers in Java
Identifiers are names for variables, classes, methods, etc., and must follow these rules:
- Can start with a letter, currency symbol, or underscore (_). Conventionally, variables should start with a lowercase letter.
- Can contain letters, digits, currency symbols, and underscores after the first character. Constants should be all uppercase.
- Case-sensitive.
- Keywords cannot be used as identifiers.
Legal identifiers: MinNumber, total, ak74, hello_world, $amount, _under_value
Illegal identifiers: 74ak, -amount
8. White Spaces in Java
Blank lines, which may contain only white spaces or comments, are ignored by the Java compiler.
9. Access Modifiers
Modifiers control the scope of classes and methods.
-
Access Modifiers:
default
,public
,protected
,private
-
Non-access Modifiers:
final
,abstract
,static
,transient
,synchronized
,volatile
,native
10. Understanding Access Modifiers
Access Modifier | Within Class | Within Package | Outside Package by Subclass Only | Outside Package |
---|---|---|---|---|
Private | Yes | No | No | No |
Default | Yes | Yes | No | No |
Protected | Yes | Yes | Yes | No |
Public | Yes | Yes | Yes | Yes |
11. Java Keywords
abstract | assert | boolean | break | byte |
case | catch | char | class | const |
continue | default | do | double | else |
enum | extends | final | finally | float |
for | goto | if | implements | import |
instanceof | int | interface | long | native |
new | package | private | protected | public |
return | short | static | strictfp | super |
switch | synchronized | this | throw | throws |
transient | try | void | volatile | while |