Overview
Java was originally developed by Sun Microsystem, started by James Gosling and launched in 1995. Currently, Java is a popular programming language, used for many technology products. We can highlight some key features of Java as follows:
- High-level programming language
- An object-oriented programming language (Object oriented). In Java, all are objects.
- Not dependent on the platform (Platform independent). Unlike many other languages such as C and C ++, when compiling a Java program it will not compile into the platform specified on the machine but compile into byte code regardless of the platform. Byte code can be run on all devices with JVM – Java Virtual Machine installed.
- It is a multithreaded language. Java programmers can write so that the program runs multiple threads simultaneously.
- High performance
Basic Syntax
The basic components in a Java program
- Object – Objects have states and behaviors.
- Class – As a framework for creating Objects, defining Object states and behaviors
- Methods – A behavior, a class has many behaviors. In methods, it is the place to process logic and manipulate data.
- Instance variables – Object has a set of duplicate instance variables. The value assigned to the entity variables creates the state of the Object
The first show
1 2 3 4 5 6 7 8 9 10 11 | <span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">MyFirstJavaProgram</span> <span class="token punctuation">{</span> <span class="token comment">/* This is my first java program. * This will print 'Hello World' as the output */</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span> <span class="token punctuation">(</span> String <span class="token punctuation">[</span> <span class="token punctuation">]</span> args <span class="token punctuation">)</span> <span class="token punctuation">{</span> System <span class="token punctuation">.</span> out <span class="token punctuation">.</span> <span class="token function">println</span> <span class="token punctuation">(</span> <span class="token string">"Hello World"</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token comment">// prints Hello World</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> |
Basic syntax rules
- Differentiate between ordinary flowers. In java, when defining Hello and hello, it will be different.
- The class name needs to be capitalized with the first letter and every word in the name needs to capitalize the first letter. EXAMPLE: MyFirstJavaProgram.class
- The method name lowercase the first letter, and the first letters of the words after it are capitalized. For example: myFirstJavaMethod ()
- Program File Name: The name of the file should exactly match the class name in the file. For example: class MyFirstJavaProgram, the file name is also MyFirstJavaProgram.java
- The Java program process starts with the main (String [] args) function, which is a must for every Java program.
Definition in Java (Java identifiers)
All components of a Java program need a name. Like class, method, variable that called definition (Identifiers). There are a few guidelines when creating definitions like this:
- All definitions must start with a letter, the $ character or the underscore (_), then may include other characters.
- The definition must not coincide with key words in Java
- Use case sensitive rules. For example: FirstJavaProgram.class, firstMethod ()
- Examples of valid definitions: doSomeThing (), abc, $ 123, _abc
- Examples of invalid definitions: 123, if, -salary
Java variables
- Local variables
- Class variables (Static variables)
- Instance variables (Non-static variables)
Array
Arrays are objects that store variables of the same type. The array itself is an object in the heap
Java Enums
Enums restrict a variable to only one value within a few predefined values. Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 | <span class="token keyword">class</span> <span class="token class-name">Phone</span> <span class="token punctuation">{</span> <span class="token keyword">enum</span> Type <span class="token punctuation">{</span> SAMSUNG <span class="token punctuation">,</span> APPLE <span class="token punctuation">}</span> <span class="token punctuation">;</span> Type type <span class="token punctuation">;</span> <span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">PhoneTest</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">void</span> <span class="token function">main</span> <span class="token punctuation">(</span> String <span class="token punctuation">[</span> <span class="token punctuation">]</span> args <span class="token punctuation">)</span> <span class="token punctuation">{</span> Phone myPhone <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Phone</span> <span class="token punctuation">(</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> myPhone <span class="token punctuation">.</span> type <span class="token operator">=</span> Phone <span class="token punctuation">.</span> Type <span class="token punctuation">.</span> SAMSUNG <span class="token punctuation">;</span> System <span class="token punctuation">.</span> out <span class="token punctuation">.</span> <span class="token function">println</span> <span class="token punctuation">(</span> <span class="token string">"My phone type: "</span> <span class="token operator">+</span> myPhone <span class="token punctuation">.</span> type <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> |
Java keyword
The keywords listed below are not used in Java definitions
abstract | assert | boolean | break |
---|---|---|---|
bytes | case | catch | char |
class | const | tiếp tục | default |
by | 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
Java supports single-line and multi-line comments. All contents in the commet will be ignored by the compiler Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">MyFirstJavaProgram</span> <span class="token punctuation">{</span> <span class="token comment">/* This is my first java program. * This will print 'Hello World' as the output * This is an example of multi-line comments. */</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span> <span class="token punctuation">(</span> String <span class="token punctuation">[</span> <span class="token punctuation">]</span> args <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// This is an example of single line comment</span> <span class="token comment">/* This is also an example of single line comment. */</span> System <span class="token punctuation">.</span> out <span class="token punctuation">.</span> <span class="token function">println</span> <span class="token punctuation">(</span> <span class="token string">"Hello World"</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> |