A Java application can be defined as a collection of objects that communicate by calling their respective methods. Let's take a look at what classes, objects, methods, and entity variables mean.
Object: Objects have state and behavior. For example: a dog has its state - color, name, breed, and also has behavior - wagging its tail, barking, and eating. An instance of an object-time class.
Class: A class can be defined as a template or blueprint that describes the behavior and state of the types supported by an object.
Method: Method is a basic behavior. There are many methods that can be included in the class. In the method, logic can be written, data can be manipulated, and actions can be performed.
Entity variables: Each object has its special set of entity variables, and the state of an object is determined by the values assigned by those entity variables.
The first Java program
Let's take a look at the following code that can output "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 this file and compile and run this program. Please follow these steps:
Open Notepad Add the above code to save the file with MyFirstJavaProgram.java. Open the command prompt window to go to the location where you saved the class. Assume it is C:/
Enter 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 and you will see "Hello World" displayed on the screen
C: > javac MyFirstJavaProgram.javaC: > java MyFirstJavaProgram
Hello World
Basic syntax
Regarding Java programs, it is important to remember a few points.
Case sensitivity: Java is a case-sensitive language, which means that Hello and hello represent different meanings in Java.
Class naming: The initial letter of all classes must be capitalized.
If the class name contains several words, the first letter of each word must be capitalized.
For example, class MyFirstJavaClass
Method naming: All method names must start with lowercase letters.
If the method name contains several words, the first letter of each word must be capitalized.
For example public void myMethodName()
Program file name: The program's file name must exactly match the class name.
But when saving the file, you should save it as the class name (note that it is case sensitive) and add the .java suffix after the file name (if the file name and class name do not match, then your program will not be able to be compiled).
For example: Assuming the class name is MyFirstJavaProgram, then the file name should be MyFirstJavaProgram.java.
public static void main(String args[]): Java programs start with the main() method, which is a mandatory part of the Java program.
Java Identifiers
All components of Java must have their own names. The names of classes, variables, and methods are called identifiers.
In Java, you need to remember the following points about identifiers. as follows:
All identifiers must begin with letters (A to Z or a to z), currency characters ($), or underscores (_).
Any combination of letters can be found after the first identifier.
Keywords cannot be used as identifiers.
Most identifiers need to be case sensitive.
Examples of legal identifiers: age, $salary, _value, __1_value
Example of illegal identifier: 123abc, -salary
Java modifiers
Like its language, methods, classes, etc. can be modified by modifiers. There are two modifiers in Java:
Access modifiers: default, public, protected, private
Non-access modifiers: final, abstract, strictfp
We will continue to learn about modifiers in the next section.
Java keywords
The following are the keywords preserved in Java. These keywords cannot be used as names for constants, variables, and other identifiers.
| Keywords | Keywords | Keywords | Keywords |
|---|---|---|---|
| abstract | assert | boolean | break |
| byte | case | catch | char |
| class | const | Continue 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 |
Comments 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"); }} Use empty lines
A line with only spaces may be a comment. Such a line is called a blank line, and Java will completely ignore it.
Basic data types
Variables are memory locations that are reserved for storing values. This means that when you create a variable, it will take up a certain amount of space in memory.
Based on the data type of variables, the operating system makes memory allocation and decides what will be stored in reserved memory. Therefore, by assigning different data types to variables, you can store integers, decimals, or letters in these variables.
There are two efficient data types in Java:
Java supports 8 primitive data types. The original data type is predefined by the language and named with keywords. Let's learn more about these 8 data types below.
Byte type (byte)
Byte type is an 8-bit binary integer with positive and negative
Byte type data types are mainly used to save space in large arrays, and are mainly used to replace integers. Because byte type is 4 times smaller than integers.
For example: byte a = 100 , byte b = -50
Short integer (short)
Short integer is a 16-bit binary integer with positive and negative
Short integer type data can also be used to save space like byte types. Short integers are twice smaller than integers
For example: short s = 10000, short r = -20000
Int type (int)
Integer type is a 32-bit binary integer with positive and negative
Integer types are generally applied to integer values by default unless you are worried about insufficient memory.
For example: int a = 100000, int b = -200000
Long type (long)
Long integer is a 64-bit binary integer with positive and negative
This data type is generally applied when a larger range than an integer type is required.
For example: long a = 100000L, int b = -200000L
Float
Floating point data is a single-precision 32-bit IEEE 754 standard floating point data.
Floating point data is mainly used to save memory in large floating point digital arrays.
Floating point data cannot be used for precise data such as currency.
For example: float f1 = 234.5f
Double precision type (double)
Double precision data is a double precision 64-bit IEEE 754 standard floating point data.
This data type is mainly used by default to represent the value of the decimal, and is generally the default choice.
Double-precision data cannot be used for precise data such as currency.
For example: double d1 = 123.4
Boolean
Boolean data represents an information bit.
It has only two possible values: true (true) and false (false)
This data type is used for simple tags under real or false conditions.
For example: boolean one = true
Character type (char)
Character data is simple 16-bit Unicode standard characters.
Character data can be used to store any letter.
For example: char letter A (character letter A) ='A'
Reference data type
The reference data type is defined by the class's editor. They are used to access objects. These variables are defined as specific types that are not alterable. For example: Employee, Puppy, etc.
For example: Animal animal = new Animal("giraffe");
Java Constants
Constants are source code representing fixed values. They are represented directly in code form without any estimates.
Constants can be assigned to any original variable type. For example:
byte a = 68;char a = 'A'
Byte, integer, long and short can also be represented by decimal, hexadecimal and octal counting systems.
When these technical systems represent direct quantities, the prefix 0 is to indicate octal, and the prefix 0x is to indicate hexadecimal. For example:
int decimal = 100;int octal = 0144;int hexa = 0x64;
The provisions of string constants in Java, like most other languages, should also be written in the middle of double quotes. Examples of string-type direct quantity are as follows:
"Hello World""two/nlines""/"This is in quotes/""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Character and string constants can contain any Unicode letters. For example:
char a = '/u0001';String a = "/u0001";
The Java language also supports special escape sequences of characters and strings directly. They are:
| Escape characters | meaning |
|---|---|
| /n | Line break (0x0a) |
| /r | Enter (0x0d) |
| /f | Page change (0x0c) |
| /b | Backspace (0x08) |
| /s | Space(0x20) |
| /t | tab |
| /" | Double quotes |
| /' | Single quotes |
| / | Backslash |
| /ddd | Octal characters (ddd) |
| /uxxxxx | Hexadecimal UNICODE characters (xxxx) |