Javase Basics-1. Data types and operators

Source: Internet
Author: User
Tags arithmetic bitwise bitwise operators

Summary of knowledge points in this chapter

Data type:

 1. Basic data types

Numeric type

Integer type

Byte (128~127), accounting for 1 bytes of storage room

Short ( -2^15~2^15-1), accounting for 2 bytes

Int (default type -2^31~2^31-1, approx. 2.1 billion), accounting for 4 bytes

Long ( -2^63~2^63-1), which accounts for 8 bytes, declares that the long type is at the end of +l/l (case L)

Floating-point types

Float (4 bytes of storage space, is a single-precision type, about 7 bits after the decimal point, declares the float type when the end of +f/f),

Double (default type, 8 bytes of storage space, double type, twice times the float precision)

Character type (char)

single quotation marks denote character constants (' A '), Unicode encoding is designed to handle all text in a variety of languages, which accounts for 2 bytes and allows 65,536 characters

ASCII code is 1 bytes, which allows 128 characters (the highest bit is 0) and is the first 128 characters in the Unicode encoding table. (ASCII table can be Baidu)

The Unicode value of the character when the char type is stored in memory, and all char types can be treated as int types.

Boolean (Boolean:true/false) is used to determine the logic conditions, generally used in program flow control

  2. Reference data types

    Classes (Class)

Interface (interface)

Array

Precautions:

1.Java is a strongly typed language, and each variable must declare its owning type.

2.Java variable is the most basic storage unit in the program, its elements include: variable type (base type or reference type) variable name (legal identifier) scope

(For Example:type VarName [=value] [{, Varname[=value]}])

3.final modified variables can only be initialized once, variable names must all be uppercase, multiple words underlined

(Foe example:public static final int max_value;)

4. Naming conventions: Variables and method names: first letter lowercase and Hump principle (getName); class Name: First letter capitalization and hump principle (Goodmen)

Operator

  Java-supported operators:

1. Arithmetic operator: +-*/% + +--

Integer operations: The default is int type, two operands have a long, the result is long, no long type is int

Floating-point arithmetic: The default is double type, one is double, the result is double, two are float type, and the result is float type

++num and num++ difference: The former is the first self +1 to participate in the next operation, the latter is the first to participate in the operation, the operation of itself +1

2. Assignment operator: = + = = *=/=%=

3. Relational operator: Boolean type, result is true or false

4. Comparison operator: The result of the comparison is a Boolean value, and floating-point types are generally not compared because of the loss of precision

5. Logical operator: Boolean or Boolean expression for logical operation

      

the difference between & and &&: & ": Expressions on both sides of & will participate in calculations in any case.

&&: When the left of && is false, the expression to the right of it will not be evaluated. False if left false

| and | | The difference between & and &&

6. Bitwise operators (Understanding):

      

Bitwise operators are operations and operations on operands in binary byte bits, and the operands and results are integers.

If the operand is char,byte,short, the displacement action is automatically promoted to the int type, and the result of the operation is the int type.

7. Conditional operator (trinocular operator):

Expression: Expression 1 (boolean expression)? Expression 2: Expression 3

The meaning is: if expression 1 is true, the result is expression 2; If expression 1 is Falsse, the result is an expression of 3

Considerations when calculating:

When assigning and arithmetic operations, the data type must be the same, otherwise data type conversions are required.

Data type conversions are divided into automatic conversions (left type > Right type) and cast (left type < right type).

      

The operation type is the most advanced type on the left, and the right is the highest type, which is automatically converted;

Byte-> short,char->int->long->float->double is type from low to high

Cast:, strong turn to pay attention to the loss of precision

For example:int n = (int) 3.14

    

Practice:

1. Enter your own name, age and gender, respectively, with a different variable to receive, and the input information to do the output. 

Public class Text1 {
Public static void Main (string[] args) {
String name;
int age;
String Gander;
//Create scanner
Scanner input = new Scanner (system.in);
System.out.println ("Please input your name here:");
name = Input.nextline ();
System.out.println ("Please input your:");
Age = Input.nextint ();
System.out.println ("Please input your gander here:");
gander = Input.next ();
System.out.println ("The computer recorded that:");
System.out.println ("Your name is:" +name);
System.out.println ("Your is:" +age);
System.out.println ("Your Gander is:" +gander);
}

}

 2. Enter the circle radius to find the circumference of the circle and the area of the circle, and output the result

public class Text1 {
public static void Main (string[] args) {
Double R;
Double l;//Perimeter
Double s;//Area
Scanner input = new Scanner (system.in);
System.out.println ("Please enter the radius of the Circle:");
R = input.nextdouble ();
L = 2 * Math.PI * r;
s = Math.PI * R * r;
System.out.println ("The radius of the circle is:" +r);
System.out.println ("The circumference of the circle is:" +l);
System.out.println ("The area of the circle is:" +s);
}

}

  

    

  

Javase Basics-1. Data types and operators

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.