Basic syntax of Java and data types supported by Java

  • 2020-05-05 11:13:57
  • OfStack

An Java application can be defined as a collection of objects that communicate by calling their respective methods. Let's take a closer look at what classes, objects, methods, and entity variables mean.

Object: an object has state and behavior. For example, a dog has its status -- color, name, breed -- but it also has its behavior -- wagging its tail, barking, and eating. Object is an instance of a class.
Classes: classes can be defined as templates or blueprints that describe the behavior and state of the types that an object supports.
Method: method is a basic behavior. Classes can contain many methods. In methods, you can write logic, manipulate data, and perform actions.
Entity variables: each object has its own set of special entity variables, and the state of an object is determined by the values assigned to those entity variables.
first Java program
let's take a look at the code below that outputs "Hello World."


public class MyFirstJavaProgram {

  /* This is my first java program. 
  * This will print 'Hello World' as the output
  */

  public static void main(String []args) {
    System.out.println("Hello World"); // prints Hello World
  }
} 

Let's see how to save the file and compile and run the program. Follow these steps:

Open notepad and add
Save the file
with MyFirstJavaProgram.java as the file name Open the command prompt window and go to the location where you saved the class. The assumption is that C: \
Type javac MyFirstJavaProgram.java in the window and press enter to compile your code. If your code has no errors, the command prompt will go to the next line (assuming the path variable is set successfully).
Now enter java MyFirstJavaProgram to run your program
You will see "Hello World"
on the screen


C : > javac MyFirstJavaProgram.java
C : > java MyFirstJavaProgram

Hello World

basic syntax
it is important to remember a few points about the Java program.

Case sensitivity: Java is a case-sensitive language, which means that Hello and hello mean different things in Java.
Class name: all class names must be capitalized.
If the class name contains several words, capitalize the first letter of each word.
Such as class MyFirstJavaClass
Method names: all method names must begin with a lowercase letter.
If the method name contains several words, capitalize the first letter of each word.
For example, public void myMethodName()
Program file name: the program file name must exactly match the name of the class.
But when you save the file, you should save it with the class name (case sensitive) and add the.java suffix to the file name (if the file name and the class name don't match, you won't be able to compile your program).
For example, if the class name is MyFirstJavaProgram, the file name should be MyFirstJavaProgram.java.
public static void main(String args[]) : Java programs are all processed from the main () method, which is a mandatory part of the Java program.
Java identifier
All components of
Java should have their own names. The names of classes, variables, and methods are called identifiers.

In Java, you need to keep the following points about identifiers in mind. As follows:

All identifiers must begin with a letter (A to Z or a to z), a currency character ($), or an underscore (_).
You can have any combination of letters after the first identifier.
Keywords cannot be used as identifiers.
Most identifiers need to be case-sensitive.
Examples of valid identifiers: age, $salary, _value, arbitration 1_value
Example of an illegal identifier: 123abc, -salary
Java modifier
, like its language, can be modified with modifiers for methods, classes, and so on. There are two modifiers in Java:

Access modifiers: default, public, protected, private
Non-access modifiers: final, abstract, strictfp
We will continue our study of modifiers in the next section.

Java keyword
The keywords reserved in Java are listed below. These keywords cannot be used as names for constants, variables, and other identifiers.

关键字 关键字 关键字 关键字
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

Comment
in Java Java supports single-line or multi-line comments like C and C++. All letters in comments are ignored by the Java compiler.


public class MyFirstJavaProgram{

  /* This is my first java program.
  * This will print 'Hello World' as the output
  * This is an example of multi-line comments.
  */

  public static void main(String []args){
    // This is an example of single line comment
    /* This is also an example of single line comment. */
    System.out.println("Hello World"); 
  }
} 

USES the blank line
A line with only Spaces on
might be a comment, such a line is called a blank line, and Java will ignore it completely.


basic data type
The
variable is the memory location that is reserved for storing values. This means that when you create a variable it takes up some space in memory.

Based on the variable's data type, the operating system allocates memory and decides what will be stored in reserved memory. Therefore, by assigning variables to different data types, you can store integers, decimals, or letters in these variables.

There are two valid data types in Java:

Raw data type references data type raw data type

Java supports eight primitive data types. The raw data types are predefined by the language and named by keywords. Let's take a closer look at these eight data types.

byte type (byte)

The byte type is an 8-bit binary integer with plus or minus

The minimum value is -128(-2^7) and the maximum value is 127(2^7-1). The default value is 0

Byte data types are mainly used to save space in large arrays, mainly to replace integers because bytes are 4 times smaller than integers.

For example: byte a = 100, byte b = -50
Short integer (short)

A short integer is a 16-bit binary integer with plus or minus

The minimum is -32768(-2^15) and the maximum is 32767(2^15-1)

Data of short integer type can also be used to save space just like byte type. Short integers are two times smaller than integers The default value is 0

For example: short s = 10000, short r = -20000

integer (int)

Integer type is a 32-bit binary integer with plus or minus

The minimum is minus 2,147,483,648(minus 2^31) and the maximum is 2,147,483,647(2^31 -1)

Integer types are generally applied to integer values by default unless memory runs out.

The default value is 0

For example: int a = 100000, int b = -200000

long integral (long)

A long integer is a binary integer

with 64 bits plus or minus The minimum value is a maximum - 9223372036854775808 (63) - 2 ^ 9223372036854775807 (2 ^ 63-1)

This data type is generally used when you need a larger range than an integer type.

The default value is 0L

For example: long a = 100000L, int b = -200000L

floating point (float)

Floating point data is a single-precision, 32-bit floating point data in the IEEE 754 standard.
Floating point data is primarily used to save memory in large arrays of floating point Numbers.

The default value is 0.0f.

Floating point data cannot be used for precise data such as currency.

For example: float f1 = 234.5f
double precision (double)

Double-precision data is a double-precision 64-bit floating point data in the IEEE 754 standard.
This data type is mainly used by default to represent decimal values, and is generally the default choice.
Double-precision data cannot be used for precise data such as currency.

The default value is 0.0d

For example: double d1 = 123.4

Boolean (boolean)

A Boolean data represents a bit of information.
It has only two possible values: true (true) and false (false)
This data type is used for simple markup under true or false conditions.

The default value is false (false)

For example: boolean one = true

character type (char)

The character data is a simple 16-bit Unicode character.

The minimum value is: '\u0000' (or 0). The maximum value is: '\uffff' (or 65,535).

Character data can be used to store any letter.

For example: char letter A (character letter A) ='A'
refers to the data type
The
reference data type is defined by the editor of the class. They are used to access objects. These variables are defined as specific types that cannot be changed. For example: Employee, Puppy, etc.

Class objects and array variables are such reference data types. The default value for any reference data type is null. A reference data type can be used for any object that declares a type and a compatible type.

For example: Animal animal = new Animal("giraffe");

Java constant
The
constant is the source code that represents a fixed value. They are represented directly in code without any estimation.
Constants can be assigned to any primitive variable type. For example:


byte a = 68;
char a = 'A'

Byte, integer, long and short integers can also be represented by decimal, hexadecimal and octal counting systems.

When these technical systems are used to represent direct quantities, the prefix 0 is for octal and the prefix 0x is for hexadecimal. For example:


int decimal = 100;
int octal = 0144;
int hexa = 0x64;

String constants in Java are specified in double quotes, as in most other languages. An example of a string type direct quantity is


"Hello World"
"two\nlines"
"\"This is in quotes\""

Characters and string constants can contain any Unicode letter. For example:


char a = '\u0001';
String a = "\u0001";

The Java language also supports some special escape sequences of characters and string direct quantities. They are:

转义字符 含义
\n 换行 (0x0a)
\r 回车 (0x0d)
\f 换页 (0x0c)
\b 退格 (0x08)
\s 空格 (0x20)
\t tab
\" 双引号
\' 单引号
\ 反斜杠
\ddd 八进制字符 (ddd)
\uxxxx 十六进制 UNICODE 字符 (xxxx)


Related articles: