Java data type and its conversion & frequently used shortcut keys, Java data type conversion
Data Type
Basic data types: byte, short, int, long, char, float, double, and boolean
Integer byte 1 byte-128 ~ 127, short 2 bytes, int 4 bytes, long 8 bytes
Character char 2-byte adopts unicode
Float 4-byte 32-bit single precision f1 = 3f; double 8-byte 64-bit double Precision
Boolean (true false)
The long type can be directly converted to the float type.
5 + 6 + 7L + 'A' -- long type
5 + 5.6*4 + 'A' -- double Type
+ L after the integer indicates the long integer -- long (1234L)
Number of decimal points (floating point number) -- double regardless of the actual value
Float (12.0F)
Conversion of Basic Data Types
Automatic type conversion: under certain conditions, the system automatically performs implicit type conversion.
① Two Types of compatibility ② the target type is greater than the original type ③ for expressions, (precision type) If an operand is of the double type, the entire expression can be upgraded to the double type. (Integer) Maximum long
Forced type conversion: manual type conversion will result in loss of Precision
① Two Types of compatibility ② the general target type is smaller than the original type
Cross-data type conversion
There are three methods to convert basic types into strings
1. Use the valueOf method of String
2. encapsulate the basic data type as an object, and then call the toString method of the object.
3. Add the basic type with an empty string to obtain the string corresponding to the basic type data.
There are two methods to convert a string to a basic type.
1. Call the parseXxx static method of the basic encapsulation class.
2. Construct an encapsulated object of the basic type with a string, and then call the xxxValue method of the encapsulated object.
Conversion of referenced data types
Shortcut Key
Syso + alt +/-- system. out. Println ()
Main and press alt +/-- public static void main (String [] args ){
\ N -- line feed \ t -- Space
SHIFT + TAB -- move forward Ctrl + A -- select Ctrl + shift + f -- automatically organize the code 1TAB = 4 spaces
1 package com. jredu. ch02; 2 3 import java. util. bonus; 4 5 6/** 7 * member lottery 8 * @ author Administrator 9*10 */11 public class Exer2 {12 13 public static void main (String [] args) {14 System. out. print ("Enter your four member card number"); 15 bytes input = new bytes (System. in); 16 // obtain the input member number 17 int num = input. nextInt (); 18 // calculate the sum of each digit in four digits. 19 // The sum of thousands of digits. 20 int a = num/1000; 21 // hundred digits 22 int B = num % 1000/100; 23 // ten digits 24 int c = num % 100/10; 25 // Single digit 26 int d = num % 10; 27 // four digits each digit and 28 int sum = a + B + c + d; 29 // output the sum of membership card numbers 30 System. out. println ("the sum of membership card numbers is" + sum); 31 System. out. println (sum> 20? "Congratulations, winning 50 RMB": "Sorry, you didn't win"); 32 input. close (); 33 34} 35 36}