Some basic concepts in Java

Tram Ho

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

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:

Java keyword

The keywords listed below are not used in Java definitions

abstractassertbooleanbreak
bytescasecatchchar
classconsttiếp tụcdefault
bydoubleelseenum
extendsfinalfinallyfloat
forgotoifimplements
importinstanceofintinterface
longnativenewpackage
privateprotectedpublicreturn
shortstaticstrictfpsuper
switchsynchronizedthisthrow
throwstransienttryvoid
volatilewhile

Comment

Java supports single-line and multi-line comments. All contents in the commet will be ignored by the compiler Example:

Chia sẻ bài viết ngay

Nguồn bài viết : Viblo