Basic Java Grammar
Java basic grammar
This page explains the basic syntax of Java.
1. Variable
Variables are containers for storing data while a Java program is running. Every variable is assigned a data type that specifies the type and amount of value it can hold. Variables are also memory location names for data. and the name given to the memory location.
Java has different types of variables such as:
| Variable | Description |
|---|---|
| String | Stores text such as "Hello." String values are enclosed in double quotes |
| int | Stores an integer (integer) without a decimal point, such as 123 or -123. |
| float | Stores a floating point number with a decimal point, such as 19.99 or -19.99. |
| char | Stores a single character, such as 'a' or 'B'. Character values are enclosed in single quotes |
| boolean | Stores a two-state value: true or false. |
1.1. Variable declaration
Variable declarations are written as follows.
変数型 変数名;
1.2. Variable assignment
Variable assignment is written as follows.
変数名 = 値;
2.Constant
Constants are basically variables whose values cannot be changed. In C/C++, the keyword const is used to declare these constant variables. Java uses the keyword final. However, the tools introduced here are not just primitive variables, but actual object instances.
3.Class
A class is a user-defined skeleton or prototype from which objects are created. It represents a set of properties or methods common to all objects of one type.
- A class is a set of objects that share common characteristics/behaviors and common properties/attributes.
- Classes are not real-world entities. It is simply a template, skeleton, or prototype from which objects are created.
- Classes do not occupy memory.
- A class is a group of variables of different data types and a group of methods.
A Java class can contain:- data member
- method
- constructor
- nested classes and interfaces
4.Object
Objects are the basic unit of object-oriented programming and represent real-world entities. A typical Java program creates many objects and, as you know, interacts with them by calling methods. The object consists of:
- State: Represented by an object’s attributes. It also reflects the characteristics of the object.
- Behavior: Represented by an object’s methods.
- Identity: Gives objects a unique name and allows one object to interact with other objects.
5. import statement
import is a Java keyword. Declare a Java class to use in the code below the import statement. Once a Java class is declared, you can use the class name in your code without specifying the package to which the class belongs. Use the ’*’ character to declare all classes that belong to a package.
import package.JavaClass;
import package.*;
6.Package
Packages in Java are a mechanism for encapsulating groups of classes, subpackages, and interfaces. Packages are used for example:
- Prevent name conflicts. For example, there are two classes named Customer in two packages: com.test.Customer and com.test.Customer.
- Easily find/find and use classes, interfaces, enumerations, annotations
- Packages can be considered data encapsulation (or data hiding)
package com.test.Customer
TulipSoft