Basic Java Knowledge

Source: Internet
Author: User
Tags arithmetic class definition java keywords

Java's core strengths: cross-platform

Jdk:java Development Kit Jre:java Runtime Environment Jvm:java virtual machine JDK contains JRE,JRE contains JVM

JAVA_HOME:JDK installation directory path: When executing a DOS command, the system will look for the executable file for the command in the path specified directory, "#Java_HOME #\bin" in the directory, separated by semicolons for different paths classpath:jdk 5.0 or more versions do not need to be set

DOS command: DIR view tag Directory

The Java language is strictly case-sensitive, and all of the keywords in the Java program are lowercase.

The Main method is the entry of the program

Args is an abbreviation for the arguments (parameter)

Note: Multiple lines of comments do not nest

Java programs are purely object-oriented programming languages, so Java programs must exist in the form of Class (Class), which is the smallest program unit of a Java program. Java programs do not allow executable statements, methods and other components to exist independently, all the program parts must be placed in the class definition.

Java identifiers (naming classes, variables, and methods) naming rules:

1. Identifiers must begin with a letter, underscore, dollar sign, or other special characters, such as @,%, etc.

2, the other parts of the identifier can be letters, underscores, dollar characters and any combination of numbers, where numbers do not begin ;

3, Java identifier case sensitive, and unlimited length, identifiers cannot contain spaces;spaces in Java include Space, tabs (tab), carriage return (enter), and so on

4, can not be Java keywords and reserved words;

Note: Java does not use the ASCII character set used in the usual language, but rather uses a standard international character set such as Unicode. Therefore, the meaning of the letter here, English, kanji and so on (do not recommend the use of Chinese characters to define identifiers)

Java source file naming rules:

1. The source file suffix must be. java

2, the main source file name is arbitrary, but if the Java source file defines a public class, the source file must be the same as the public class name

So a Java source file can only define one public class.

data types for Java:

The Java language is a strongly typed language, and the strongly typed language contains two meanings: 1. All variables must be declared first, then used; 2, variables of the specified type can only accept values that match the type. This means that each variable and each expression has a type that is determined at compile time.

Strongly typed languages can reduce programming errors by making more rigorous syntax checks at compile time.

Declaring variables is simple, specifying variable types and variable names. (when defining a variable, you can specify the initial value, or do not specify the initial value, regardless of the variable, at least the variable type and the variable name must be specified)

The types supported by the Java language are divided into two categories: the base type and the reference type.

Reference types include classes, interfaces, and array types. There is also a special type of NULL. The so-called reference type is a reference to an object, including instances and arrays of two

The base types include Boolean types and numeric types. Numeric types have integer types and floating-point types.

Integer types include:

1, byte: A byte type integer in memory 8 bits (one byte), the number of tables range-128 (-27)-127 (27-1);

2, Short: A short type integer in memory accounted for 16 bits (2 bytes), the number of tables range-32768 (-215)-32767 (215-1);

3, int: An int type integer in memory occupies 32 bits (4 bytes), the number of table range (-231)-(231-1);

4, Long: A long type integer in memory 64 bits (8 bytes), the number of table range (-263)-(263-1);

int is the most commonly used integer type and, in general, gives an integer value directly by default to the int type. Unless: 1, directly assign a small integer value directly to a byte or short variable, the system will automatically treat as this type;

2, if a large integer value, beyond the range of int table, the system will not automatically treat this integer value as a long type, should be added after the value L or L. to avoid confusion with the number 1, it is recommended to use uppercase L.

 Public class test{    publicstaticvoid  main (string[] args) {        // compilation error, indicating large integer         // long L = 300000000000         // can be compiled normally        long L = 300000000000L;        System.out.println (l);        }    }

character char is actually an integer type, equivalent to an unsigned integer type.

Floating-point types include float and double

Double is 8 bytes, 64 bits; float takes 4 bytes, 32 bits.

Only floating-point types can be represented by scientific notation.

The floating-point type is the default double type, and if you want Java to treat a floating-point type as a float type, it should be followed by F or F after this floating-point type value.

Java also provides three special floating-point values: positive infinity, negative infinity, and non-number to indicate overflow or error. Positive infinity is represented by the positive_infinity of a double or float class, where the negative infinity is represented by the negative_infinity of the double or float class, and the non-number is represented by the Nan of the double or float class. All positive infinity values are equal, negative infinity values are equal, non-numbers are equal to any numeric value, and even Nan are not equal.

Floating-point numbers have errors, and if you need a precise floating-point number, you can use the BigDecimal class.

Only the floating-point number is divided by 0 to get positive infinity or negative infinity, because the Java language automatically treats 0 (integers) of the floating-point arithmetic as 0.0 (floating-point numbers). If an integer is divided by 0, an exception is thrown: Airthmeticexception:/by zero (divided by 0 exception).

Boolean type

The Boolean has only one Boolean type, which is used to represent the logical "true" or "false", the numeric value of the Boolean type can only be true or flase, cannot be represented by 0 or 0, and other numeric types cannot be converted to Boolean types.

The string "true" and "false" are not converted directly to a Boolean type, but if you use a Boolean type of value and string for the join operation, the value of the Boolean type is automatically translated into a string.

 Public class test{    publicstaticvoid  main (string[] args) {        Boolean  false;         if true // here re-assign true to B, so it will always be set up            System.out.println ("hehe");     }}}

char character type

There are three representations of the character type:

1, directly through a single character to specify the character type value;

2. A special character type is represented by a transfer character; If you need to output a quotation mark, if the nesting cannot be compiled, use the transfer character to

 Public class test{    publicstaticvoid  main (string[] args) {        // escape character \ Output quotation mark        char c1 = ' \ ';        SYSTEM.OUT.PRINTLN (C1);        }    }

3, directly through the Unicode value to represent the character type value; a single character, a single character, a single character, an important thing to say three times

 Public classtest{ Public Static voidMain (string[] args) {//Char is a single character, and the following sentence cannot be compiled, prompting for an unfinished character literal//char a = ' ggsag ';        CharC1 = ' A '; CharC2 = ' B '; //output aSystem.out.println (C1); //Output 195SYSTEM.OUT.PRINTLN (C1 +C2); }    }
 public  class   test{ public  static  void   Main (string[] args) { char  c1 = ' A ' ;        SYSTEM.OUT.PRINTLN (C1);  //  Each character has a corresponding number, the output is  int  i = C1;        System.out.println (i);  //  cast int type to char type  char  c2 = (char  ) I;        System.out.println (C2); }    }
the values are separated by a draw line

Whether it is an integer or a floating-point value, you can use the drawing line freely, and it is more straightforward to see how many bits are included.

/*     use lower line split in numeric value */Publicclass  test{    publicstatic  void  main (string[] args) {        long l = 3000_0000_0000l;         long a = 4111_1111_1111l;         + a);         // Output: 711111111111         }    }

For loop printing from A to Z

 Public class test{    publicstaticvoid  main (string[] args) {        char C1 = ' a ';          for (int i = 0; i <; i++) {            char temp = (char) (C1 + i);            SYSTEM.OUT.PRINTLN (temp);}}    }
type conversions for basic types

There are two ways of transforming: automatic type conversion and forced type conversion

The conversion of the int type to the Float,long type to float and double can be automatically converted, but may result in a loss of precision.

All data types in Java can be converted to each other, and this is an automatic type conversion if the system assigns a value of a primitive type directly to a variable of another base data type. When assigning a number or variable with a small range of tables to another variable with a large range of tables, the system will automatically convert the type, otherwise it will need to be cast.

// If you want to convert the value of the base data type to the corresponding string, you can concatenate the value of the base data type with a null character // Output 4Hello // Output Hello44

Typically, a string cannot be converted directly to a base type, but a wrapper class that corresponds to a base type can be used to convert a string to a basic type

 Public class stringtest{    publicstaticvoid  main (string[] args) {        = ""  ;         int i = integer.parseint (str);        System.out.println (i);        }    }

Java provides the corresponding wrapper class for the basic data types in 8: boolean-boolean;byte-byte;short-short;int-integer;long-long;char-character;float-float ; double-double,8 a wrapper class provides a parsexxx (string str) static method for converting a string to a base type.

When an arithmetic expression contains values of more than one primitive type, the entire arithmetic expression data type is automatically promoted, with the following rule:

1. All byte type, short type, char type will be promoted to int type;

2. The data type of the entire arithmetic expression is automatically raised to the same type as the highest-level operand in the expression.

int a = 3; int b = 23/A; // 23 except 3 can not be exhausted, but still get an int type integer // The result is an integer that truncates the fractional part to rounding

If the expression contains a string, then:

// Output Helloa7 System.out.println ("Hello" + ' a ' + 7); // Output 104Hello SYSTEM.OUT.PRINTLN (7 + ' a ' + "Hello");
Direct Volume

Direct volume refers to the value given in the program through the source code.

There are three types of direct quantities that can be specified: base type, string type, and null type. Java supports direct quantities of types in 8: int, long, float, double, Boolean, char, String, NULL

The direct amount of a stirng type cannot be assigned to a variable of another type, and the direct amount of the null type can be directly assigned to any variable of the reference type.

Chang: Some of the data in the compiled. class file that is determined at compile time, including constants in classes, methods, interfaces, and string literals.

    // Java ensures that each string constant has only one and does not produce multiple copies    String s0 = "Hello";     = "Hello";     = "He" + "Llo";     = = S1)    ; = = s2)    ; // the above two lines will both output true
Three mesh operator

The three-wood operator has only one:? :

String str = 5 > 3? "5 is greater than 3" : "5 not greater than 3"; System.out.println (str);

Trinocular operators can be nested

a > B? "A greater Than B": (A < b?) "A is less than B": "A equals B")

Basic Java Knowledge

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.