Table of contents
Java Identifiers ✨
Java Identifiers are names given to variables, classes, methods, and other entities in a Java program. They are used to identify and refer to these entities in the program.
Here are the rules and guidelines for defining Java Identifiers:
Java Identifiers must start with a letter (uppercase or lowercase), a dollar sign ($), or an underscore (_).
After the first character, Java Identifiers can contain any combination of letters, digits, dollar signs, and underscores.
Java Identifiers are case sensitive, meaning that an identifier named "myvariable" is different from "Myvariable".
Java Identifiers cannot be a reserved word in the Java language. Some examples of reserved words include "class", "public", "private", "void", and "main".
Java Identifiers can be of any length, but it is recommended to keep them descriptive and meaningful, while also keeping them short.
It's also important to note that Java has a naming convention that should be followed when naming identifiers. For example, class names should start with a capital letter and follow camel case (e.g. MyClassName), while variables should start with a lowercase letter and also follow camel case (e.g. myVariableName).
These are the basic rules for defining Java Identifiers. Following these rules will ensure that your program runs without errors and is easily readable and maintainable.
Java Variable ✨
A variable is a container that holds a value that can be changed during the execution of a program. A variable must be declared before it can be used, and its type determines what kind of values it can hold.
In Java, a variable is declared using the following syntax:
<data type> <variable name>;
For example, to declare an integer variable named "age", you would use the following code:
int age;
You can also assign a value to the variable when you declare it, like this:
int age = 30;
In this example, the integer variable "age" is declared and assigned the value 30 in the same statement.
It's important to note that you can only assign a value to a variable that is compatible with its data type. For example, you can't assign a string value to an int variable, or a floating-point value to a boolean variable.
Here are some more examples of variable declarations in Java:
String name = "John Doe";
double salary = 75000.00;
char grade = 'A';
boolean isStudent = false;
In these examples, a string variable named "name" is declared and assigned the value "John Doe", a double variable named "salary" is declared and assigned the value 75000.00, a character variable named "grade" is declared and assigned the value 'A', and a boolean variable named "isStudent" is declared and assigned the value false.
In Java, there are two main types of variables: instance variables and local variables.
Instance Variables: Instance variables are variables in Java that belong to an instance of a class, meaning that each object created from the class will have its own set of instance variables.
Here are some key points to remember about instance variables:
Declaration: Instance variables are declared inside a class, but outside of any method. They are declared using the same syntax as local variables, with the data type followed by the variable name.
Scope: Instance variables have a class-level scope, meaning that they can be accessed from anywhere within the class.
Initialization: Instance variables are automatically initialized to default values when an object is created from the class. The default value depends on the data type of the variable. For example, numeric variables are initialized to 0, boolean variables are initialized to false, and reference variables are initialized to null.
Object-specific values: Each instance of a class has its own set of instance variables, meaning that each object can have different values for the same variables. This allows you to store different data for different objects.
Access modifiers: Instance variables can be declared with access modifiers, such as "public" or "private", that determine who can access the variables and from where. By default, instance variables are declared with the "private" access modifier, meaning that they can only be accessed from within the class.
Instance methods: Instance variables can be accessed from instance methods, which are methods that belong to an instance of a class. This allows you to read and modify the values of the variables from within the methods.
Overall, instance variables are an important part of object-oriented programming in Java and allow you to store object-specific data that can be accessed from anywhere within the class.
Local Variables: Local variables are variables declared inside a method, constructor, or a block of code. They are used to store intermediate values and are created and destroyed each time the method, constructor, or block is executed.
Here are some key points to remember about local variables:
Scope: Local variables are only visible and accessible within the method, constructor, or block in which they are declared. They are not accessible from other methods or outside the block of code.
Initialization: Local variables must be initialized before they can be used. If you try to use a local variable without initializing it, the Java compiler will generate an error.
Lifetime: Local variables are created when the method, constructor, or block of code is executed, and they are destroyed when the execution of the method, constructor, or block is complete.
Purpose: Local variables are used to store intermediate values and to pass values between different parts of the code. They are often used as loop counters, accumulators, and temporary storage for values.
Naming Conventions: Local variables follow the same naming conventions as other variables in Java, including the use of letters, numbers, and underscores, and starting with a lowercase letter.
Example: Here is an example of how you might use local variables in a Java method:
public void printSum(int a, int b) { int sum = a + b; System.out.println("The sum is: " + sum); }
There are also two special types of variables in Java:
Static Variables: These are class-level variables that are shared by all instances of a class. They are declared using the "static" keyword and are often used to store values that are common to all objects of a class.
Final Variables: These are variables that are declared using the "final" keyword and cannot be changed once they are assigned a value. Final variables are often used to declare constant values that should never change during the execution of a program.
In addition to these types of variables, there are also parameters, which are special kinds of variables that are passed to a method as arguments. They work similarly to local variables, but are created and destroyed each time the method is called.
Java Modifier ✨
In Java, modifiers are keywords that are used to specify the access level and behavior of classes, methods, variables, and other elements of the program. There are several types of modifiers in Java, including:
Access Modifiers: Access modifiers are used to control the visibility of classes, methods, and variables. Java has four access modifiers: "public", "private", "protected", and "default".
Non-Access Modifiers: Non-access modifiers are used to specify the behavior of classes, methods, and variables, but do not control their visibility. Some of the non-access modifiers in Java include "static", "final", "abstract", "synchronized", "native", and "strictfp".
Here is a brief explanation of each type of modifier:
Access Modifiers:
"public": A public class, method, or variable can be accessed from anywhere in the program.
"private": A private class, method, or variable can only be accessed within the same class.
"protected": A protected method or variable can be accessed within the same class and its subclasses.
"default": A default class, method, or variable can only be accessed within the same package.
Non-Access Modifiers:
"static": A static method or variable is associated with the class, not with any specific instance of the class.
"final": A final method or variable cannot be overridden or changed.
"abstract": An abstract class or method is incomplete and must be implemented by a subclass.
"synchronized": A synchronized method or block of code can only be accessed by one thread at a time.
"native": A native method is implemented in a language other than Java.
"strictfp": A strictfp class, method, or variable ensures that floating-point calculations are performed in a platform-independent manner.
It's important to note that multiple modifiers can be used in a single declaration. For example, you might declare a method as both "public" and "final", or a variable as "private" and "static". The order in which the modifiers are declared doesn't matter, as long as they appear in the correct places in the declaration.