Introduction to Java zero foundation 3: Java data types

nezha 2021-11-25 17:09:58

brief introduction :Java Quality creators in the field 、CSDN The author of the official account of the No 、Java Architects, strivers

Fan benefits , flash sale  《Java Basic tutorial series 》 10% off time limit

Fan benefits , flash sale  《Spring Boot Basic course 》 10% off time limit

Scan the QR code on the left side of the home page , Join the group chat , Learning together 、 Progress together  

Welcome to thumb up Collection Leaving a message.  

Catalog

One 、 Main method

Two 、Java Realization hello world

3、 ... and 、 use static The decorated method is the static method

Four 、 compile java file

1、 adopt javac compile java file

2、 Execute the compiled java file

3、 How to run class In the document main Method

5、 ... and 、 data type

1、 Four integers

2、 floating-point

3、char

4、boolean

2、 Code instance

6、 ... and 、 Big numbers

7、 ... and 、 Special escape characters


One 、 Main method

public static void main(String[] args) {
}

stay main Two modifiers are used in public and static,public Modifier so that it can be accessed from anywhere in the application , Static modifiers make it a class method , You can call directly with the class name .

The return value is void, Because this access does not need to return a value .

Its name is main, Parameter is String[] args. 

Two 、Java Realization hello world

Need illustration

3、 ... and 、 use static The decorated method is the static method

Need illustration

package com.nezha.javase;
public class Test {
private void test(){
System.out.println(" Hello , I'm Nezha ");
}
public static void main(String[] args) {
Test.test();
}
}

You can use the class name . Access in the form of a method name static Method , But you can't access non static methods .

Four 、 compile java file

1、 adopt javac compile java file

javac Test.java

Compiled file Test.class

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.nezha.javase;
public class Test {
public Test() {
}
public static void main(String[] var0) {
System.out.println("hello world");
}
}

2、 Execute the compiled java file

java Test

Output results :hello world

3、 How to run class In the document main Method

Suppose the following directory has a class file , And it has main Method :

E:\nezha\JavaseProject\com\nezha\javase\Test.class

First of all to enter cmd Command line :

cd  /d  E:\nezha\JavaseProject
java   com\nezha\javase.Test

You can also run with parameters :

java   com.nezha.javase.Test     com.nezha.javase.Test   nezha

5、 ... and 、 data type

Java It's a strong type of language , Yes 8 Basic types , among 4 Integers ,2 Floating point ,1 It is used to express Unicode The character type of the coded character unit char、1 Kind of boolean type .

1、 Four integers

  1. int     4 byte    -2147483648~2147483647
  2. short 2 byte    -32768~32767
  3. long  8 byte     Very big
  4. byte  1 byte    -128~127

Usually int The most commonly used ,byte and short Type for specific applications , For example, the underlying file processing or large arrays that need to control the amount of storage space .

Long integers have a suffix L or l( Such as 123456789L). Hexadecimal values have a prefix 0x or 0X( Such as 0xFFFF).

Octal has a prefix 0, for example 010 Represents... In octal 8.

2、 floating-point

  1. double 8 byte
  2. float 4 byte

double The numerical accuracy is float Twice as many ,double It can also be called double precision floating point .

Generally used double, Don't use float.

3、char

char Type represents a single character .

char Literal values of types are enclosed in single quotation marks .

for example 'A' Yes, the encoding value is 65 The corresponding character constant . It is associated with "A" Different ,"A" Represents a string A.

char The type can be expressed as a hexadecimal value , Range from \u0000 To \Uffff.

4、boolean

boolean The type has only two values ,true and false.

Generally used for logical judgment 、 Return value .

6、 ... and 、 Data type defaults

1、 Default value of basic data type

data type The default value is
int 0
short 0
long 0
byte 0
double 0.0
float 0.0
char 0
boolean false

2、 Code instance

package com.nezha.javase;
public class Test {
private static int i;
private static short s;
private static byte b;
private static long l;
private static char c;
private static float f;
private static double d;
private static boolean bb;
public static void main(String args[]){
System.out.println(i);//0
System.out.println(s);//0
System.out.println(b);//0
System.out.println(l);//0
System.out.println("CSDN"+c+" Which zha ");//CSDN Which zha
System.out.println(f);//0.0
System.out.println(d);//0.0
System.out.println(bb);//false
}
}

char The default value is not empty ;

char The default initialization value of type is \u0000, That is, the space .

6、 ... and 、 Big numbers

If the basic shaping and floating point precision cannot meet the requirements , have access to java.math In bag BigInteger and BigDecimal.

BigInteger The integer operation with arbitrary precision is realized .

BigDecimal Realize the floating-point number operation of any precision .

have access to BigInteger.valueOf Convert integer type to BigInteger.

int a = 100;
BigInteger bigInteger = BigInteger.valueOf(a);

BigInteger Cannot be used in +、-、*、/.

Only use add.

illustrations

7、 ... and 、 Special escape characters

Escape character meaning
\b Backspace
\t Tabulation
\n Line break
\r enter
\" Double quotes
\' Single quotation marks
\\ The backslash

8、 ... and 、【 Fan benefits 】

Book delivery , We have been looking forward to it for a long time Vue!

【 Book delivery 】

1、 Top of the community scoreboard 5 name , Take a lucky person at random

2、 Community scoreboard 6-15 name , Take a lucky person at random

3、 Community scoreboard 16-30 name , Take a lucky person at random

【 How to earn points 】 Post in Nezha community 、 give the thumbs-up 、 Comments earn points !

Community entrance :【 Card printing day14】java Principle of dynamic agent

The following books , One out of four !

1、CKACKAD Exam Guide : from Docker To Kubernetes Complete strategy

2、Axure RP 9 Complete self study course

3、Spring Cloud Alibaba Micro service practice

4、Halcon Machine vision algorithm principle and programming practice

Last one :Java Zero Basics 2:java object-oriented

Next : Coming soon

Add wechat , remarks 1024, Give Java Learning route mind map  

Please bring the original link to reprint ,thank
Similar articles

2021-11-25

2021-11-25

2021-11-25