Java Basics Java Basic syntax and Java supported data types _java

Source: Internet
Author: User
Tags modifiers reserved save file java keywords

Java applications can be defined as collections of objects that communicate by invoking their own methods. Let's look specifically at what the class, object, method, entity variable means.

Object: Object has state and behavior. For example: A dog has its state-color, name, variety, and behavior-wagging its tail, barking, eating. Object is an instance of the class.
Class: A class can be defined as a template or blueprint that describes the behavior and state of the type supported by the object.
Method: The method is a basic behavior. You can include many methods in a class. In a method, you can write logic, manipulate data, and perform actions.
Entity variables: Each object has a collection of its 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 following code to output "Hello world".

public class Myfirstjavaprogram {/* is the My

  -i-i-A-Java program. 
  * This would print ' Hello world ' as the output/public

  static void Main (String []args) {
    System.out.println (" Hello World "); Prints Hello World
  }
} 

Let's take a look at how to save this file and compile and run the program. Follow these steps:

Open Notepad to add the above code
Save file with Myfirstjavaprogram.java as file name
Open the Command Prompt window and go to the location where you saved the class. Assuming it's 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 that the path variable is set successfully).
Now enter the Java Myfirstjavaprogram to run your program
You'll see "Hello World" on the screen.

C: > Javac myfirstjavaprogram.java
C: > Java Myfirstjavaprogram
Hello World

Basic syntax
It's important to remember a few things about Java programs.

Case Sensitivity: Java is a case-sensitive language, which means that Hello and hello represent different meanings in Java.
Class is named: The first letter of all classes must be capitalized.
If the class name contains several words, the first letter of each word is capitalized.
such as Class Myfirstjavaclass
Method is named: All method names must begin with a lowercase letter.
If the method name contains several words, the first letter of each word is capitalized.
For example, public void Mymethodname ()
program file name: The file name of the program must match the name of the class exactly.
But when you save the file, you should save it with the name of the class (note case sensitive) and add the. java suffix after the file name (if the filename and class name do not match then you will not be able to compile your program).
For example, if the class name is Myfirstjavaprogram, then the filename should be myfirstjavaprogram.java.
public static void Main (String args[]): Java programs are processed from 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 a few points about identifiers. As follows:

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

Access modifiers: default, public, protected, private
Non-access modifiers: final, abstract, STRICTFP
We'll continue to learn about modifiers in the next section.

Java keywords
The following is a list of reserved keywords in Java. These keywords cannot be used as names for constants, variables, and other identifiers.

Key Words Key Words Key Words Key Words
Abstract Assert Boolean Break
Byte Case Catch Char
Class Const Continue Default
Todo 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

Annotations in Java
Java supports Single-line or multiline annotations like C and C + +. The letters in all comments are ignored by the Java compiler.

public class myfirstjavaprogram{/* This is the My

  -my-A-Java program.
  * This would print ' Hello world ' as the ' output '-' is ' a
  example of multi-line comments.
  *

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

Use blank lines
a line with only a blank space may be a comment, and such a row is called a blank line, and Java ignores it entirely.


Basic data types
A variable is a memory location that is reserved for storing values. This means that when you create a variable, it takes up a certain amount of space in memory.

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

There are two kinds of valid data types in Java:

    • Raw data type
    • Reference data type
    • Raw data type

Java supports 8 of original data types. The original data type is predefined by the language and is named after the keyword. Let's take a closer look at these 8 types of data.

byte type (in byte)

The byte type is a 8-bit positive binary integer

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

byte-type data types are primarily intended to conserve space in large arrays, primarily as substitution integers, which are 4 times times smaller than integers.

Example: Byte a = m, byte B =-50
Short integer (shorter)

A short integer is a 16-bit positive binary integer

    • The minimum value is-32768 ( -2^15)
    • The maximum value is 32767 (2^15-1)

Data of short integer types can also be used to save space, like byte type. Short integers twice times smaller than integers

    • The default value is 0

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

Integer type (int)

An integral type is a 32-bit positive binary integer

    • The minimum value is-2,147,483,648 ( -2^31)
    • The maximum value is 2,147,483,647 (2^31-1)

The integer type is typically applied to integer values unless you are worried about insufficient memory.

    • The default value is 0

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

Long integer

A long integer is a 64-bit positive binary integer

    • The minimum value is-9,223,372,036,854,775,808 ( -2^63)
    • The maximum value is 9,223,372,036,854,775,807 (2^63-1)

This type of data is generally applied when it needs to be larger than the integer range.

    • The default value is 0L

Example: Long a = 100000L, int b = -200000l

Floating-point type (float)

Floating-point data is a single-precision floating-point data under the 32-bit IEEE 754 standard.
Floating-point data is intended primarily to conserve memory in large floating-point numeric arrays.

    • The default value is 0.0f.

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

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

Double data is a double-precision floating-point data under the 64-bit IEEE 754 standard.
This data type is primarily the default value that is used to represent decimals, which is generally the default choice.
Double data cannot be used for precise data such as currency.

    • The default value is 0.0d

For example: double D1 = 123.4

Boolean (Boolean)

The 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 tags under true and false conditions.

    • Default value is False (false)

For example: boolean one = True

Character type (char)

Character data is a simple character under the 16-bit Unicode standard.

    • 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 type a) = ' a '
Reference Data type
reference data types are 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 and so on.

    • class objects and array variables are the reference data types.
    • The default value for any reference data type is null.
    • A reference data type can be used with objects of any declared type and compatible type.

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

Java Constants
A constant is the source code that represents a fixed value. They are represented directly in the form of code without any estimate.
Constants can be assigned to any of the original variable types. For example:

byte a =;
char a = ' a '

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

When using these technical systems to represent the direct amount, prefix 0 is to mark Octal, and the prefix 0x is to mark hexadecimal. For example:

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

The rules for string constants in Java, like most other languages, are written in the middle of double quotes. Examples of string direct quantities are as follows:

"Hello World", "
two\nlines"
"\" is in Quotes\ ""

Character and string constants can contain arbitrary Unicode letters. For example:

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

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

Escape Character meaning
\ n Line Wrap (0x0a)
\ r Carriage return (0x0d)
\f Page Change (0x0c)
\b BACKSPACE (0x08)
\s Space (0x20)
\ t tab
\" Double quotes
\' Single quotation mark
\ Back slash
\ddd octal character (DDD)
\uxxxx Hexadecimal UNICODE character (xxxx)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.