Java Chapter II

Source: Internet
Author: User
Tags arithmetic arithmetic operators natural logarithm

Chapter II Notes

What is a variable:
A variable represents a memory area, and the variable type is different, and the size of this piece of memory is different.
#在编程语言里面, you can define variables, add data to memory, or modify existing data in memory. To put it bluntly, a variable is a place where data is stored when the program runs


Storage of data:
disks, memory cards, USB drives, CDs, memory sticks, solid state, mechanical hard drives, etc.

Simple information (temporary information) paper

People put information in the brain

How variables are stored in memory

Data is stored in memory how to find
Allocated memory will have memory address (memory address bad memory)
: The data stored by the variable name can be easily and quickly found


What are the common data types defined by Java?
Bytes: #是计算机信息技术用于计量存储容量的一种计量单位,
#也表示一些计算机编程语言中的数据类型和语言字符
8 bits constitute 1 "bytes", which is the basic unit of measurement for storage space.
1 bytes can store 1 English letters or half Chinese characters, in other words, 1 Chinese characters occupy 2 bytes of storage space
Bit: is the smallest unit of data in an electronic computer. The status of each bit can only be 0 or 1

The data is also represented in 0 and 1, (this represents only one data point, not a number), a 0 or 1 is a "bit", and the system specifies 8 bits as one byte
One byte equals 8 bits
(One byte is an English letter so big half of a Chinese character)
All the data in the computer is stored in binary number 1 or zero, a 1 or 0 called 1bit is 1 bits, 8 bits binary number is called a byte, namely byte!
A character can store a Chinese character
A Chinese character occupies two bytes

#术语: Bits, bytes are used to describe computer data units or computer storage units. The main point here is the storage unit
# The smallest unit of storage is called bit (bits):
# can only hold one of two values (0 or 1) and cannot store more information in one bit. Bits are the basic unit of computer storage.
# for a given computer design, word (word) is a natural storage unit.
# One English character and English punctuation takes one byte, one Chinese character and the other punctuation 2 bytes
#
The # Byte (byte) is a commonly used computer storage unit.
The standard definition of the # byte: one byte is 8 bits. Since each bit mentioned above is either 0 or 1, a 8-bit byte contains 256 possible 0,1 combinations


Have you previously talked about centralized data types?

There are 8 Java basic data types, so just remember. Except that it's all a reference type.
Java four class eight basic data types (bytes occupied)


#在栈中可以直接分配内存的数据是基本数据类型

BYTE size
First Category: Integer type
int (integer type) 4
Byte (byte type) 1
Short (small plastic) 2
Long (Chang Plastic) 8
Class II: Floating-point type
Double (double type dual precision) 8
Float (floating-point type single precision) 4
Class III: Logical Type
Boolean (Boolean) he has only two values of 1. True 2.False) 1
Class Fourth: Character type
char (character type) 2

Primary type size Minimum maximum value
Boolean 1 Bit--
Char 16 bit 02 of 16 square -1=65535
BYTE 8-bit -128+127
Short 16-bit-2 of 15 square =-32768+2 15 square-1
int 32-bit-2 of 31-square +2 31-square-1
Long 64-bit-2 of 63-square +2 63-square-1
Float 32-bit 1.4e-453.4028235e38
Double 64-bit 4.9e-3241.7976931348623157e308

Byte
System.out.println ("base type: Byte bits number:" + byte.size);
SYSTEM.OUT.PRINTLN ("Minimum value:" + byte.min_value);
SYSTEM.OUT.PRINTLN ("Maximum value:" + byte.max_value);
System.out.println ();


Short
System.out.println ("Basic type: Short bits number:" + short.size);
SYSTEM.OUT.PRINTLN ("Minimum value: short.min_value=" + short.min_value);
SYSTEM.OUT.PRINTLN ("Maximum value: short.max_value=" + short.max_value);
System.out.println ();

Int
System.out.println ("base type: int bits number:" + integer.size);
SYSTEM.OUT.PRINTLN ("Minimum value: integer.min_value=" + integer.min_value);
SYSTEM.OUT.PRINTLN ("Maximum value: integer.max_value=" + integer.max_value);
System.out.println ();

Long
System.out.println ("Basic type: Long bits number:" + long.size);
SYSTEM.OUT.PRINTLN ("Minimum value: long.min_value=" + long.min_value);
SYSTEM.OUT.PRINTLN ("Maximum value: long.max_value=" + long.max_value);
System.out.println ();

Float
System.out.println ("Basic type: float bits number:" + float.size);
1.4 Times 10 of the negative 45, where the E is not the natural logarithm, but the meaning of the second square of 10. It's a notation for scientific notation.
SYSTEM.OUT.PRINTLN ("Minimum value: float.min_value=" + float.min_value);
SYSTEM.OUT.PRINTLN ("Maximum value: float.max_value=" + float.max_value);
System.out.println ();

Double
System.out.println ("Basic type: Double bits number:" + double.size);
SYSTEM.OUT.PRINTLN ("Minimum value: double.min_value=" + double.min_value);
SYSTEM.OUT.PRINTLN ("Maximum value: double.max_value=" + double.max_value);
System.out.println ();

Char
System.out.println ("base type: char bits number:" + character.size);
Output Character.min_value to the console in numerical form rather than as a character
System.out.println ("min: character.min_value=" + (int) character.min_value);
Output Character.max_value to the console in numerical form rather than as a character
SYSTEM.OUT.PRINTLN ("Maximum value: character.max_value=" + (int) character.max_value);

Declaration and use of variables
There are two ways to define a variable:
Syntax: variable type variable name = value;

Define the re-assignment first
int A;
A = 10;

Edge Definition Edge Assignment
int a = 10;

The variable name must be standardized:
Letters, underscores, the remainder of the symbol, any character, underscore, number, $ symbol
Usually the first letter is lowercase, followed by the first letter of the word capital: MySchool (the variable name to make sense, see name)

Assignment operator (=):
Variable name = Expression
Assignments in the program are assigned to the left of the right
int a = (1 + 2) * (8-2);

Arithmetic operators
+: Add
-: Minus
X: Multiply
/: Except
%: Remainder

Type conversion:
Automatic type conversion
Integer, real (constant), character data can be mixed. Operations, different types of data are converted to the same type first, and then the operation is performed. The
transitions from low to advanced.
Low------------------------------------> High
byte,short,char-> int-> long-> float-> Double
Data type conversions must meet the following rules:
1. You cannot type convert a Boolean type.
2. You cannot convert an object type to an object of an unrelated class.
3. Forced type conversions must be used when converting large-capacity types to small-capacity types.
4. The conversion process may result in overflow or loss accuracy, for example:
int i = 128;
Byte B = (byte) i;
because the byte type is 8 bits and the maximum value is 127, it causes an overflow when cast to the value 128 of the INT type.
5. The conversion of floating-point numbers to integers is obtained by discarding decimals, rather than rounding, for example:
(int) 23.7 = =;
(int) -45.89f = = -45

Rule one: If one operand is of type double, the entire expression is referred to as double

Rule two: Satisfy the automatic conversion type
Char c1= ' a ';//define a char type
int i1 = C1;//char automatic type conversion to int
SYSTEM.OUT.PRINTLN ("Char automatic type is converted to int after the value equals" +i1);
char c2 = ' a ';//define a char type
int i2 = C2+1;//char type and int type calculation
System.out.println ("char type and int calculated value equals" +i2 ");
Parsing: The value of the C1 is the character ' a ', the ASCII code table shows that the corresponding int type value is, ' a ' corresponds to a value of 65, so i2=65+1=66
Extended:
ASCII (American Standard Code for information Interchange, US Information Interchange standards codes) is a computer coding system based on the Latin alphabet, mainly used to display modern English and other Western European languages. It is now the most versatile single-byte encoding system and is equivalent to ISO/IEC 646

To force type conversions:
Syntax: (type name) expression
Forcing type conversions
1. The condition is that the data type of the conversion must be compatible.
2. Format: (type) value type is an instance of the data type to enforce type conversion:
Instance
public class qiangzhizhuanhuan{
public static void Main (string[] args) {
int i1 = 123;
byte B = (byte) i1;//coercion type conversion to byte
SYSTEM.OUT.PRINTLN ("int forces the value of type conversion to Byte equals" +b);
}
}
Implicitly enforcing type conversions
1. The default type for integers is int.
2. This is not the case with floating-point types, because the float type must be followed by F or F after the number.
This section explains the basic data types of Java. The next section explores the different types of variables and their usage


logical operators
&&: Two conditions at the same time true, the result is true
|| : Two conditions one is true and the result is true
! : The condition is true, the result is false, the condition is false, the result is true


Relational operators
> Greater than
< less than
= equals
>= greater than or equal to
<= less than or equal to
! = does not equal
The function of the relational operator: used to do the comparison operation, the comparison result is a Boolean


Precedence of Operators
Maximum parentheses ()
Minimum assignment =

Precedence order: Arithmetic operators > Relational operators > Logical operators > Assignment operators

Short a = 1;
Short B = 2;
So what kind of a+b is it?
A: In the Java world, if you perform operations that are smaller than the type int, Java compiles them uniformly and strongly into int types. When the type is larger than the int type, it is automatically converted to the largest type in them.

String A1 = "Zhang San";//000001

String A2 = "Zhang";//000001

if (a1 = = A2) {//Compare addresses
System.out.println ("True");
}else{
System.out.println ("False");
}

if (A1.equals (A2)) {//Compare values are equal
System.out.println ("True");
}else{
System.out.println ("False");
}

}

Java Chapter II

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.