Java primitive data types include short, byte, int, long, Boolean, char, float, and double. Raw data is unprocessed or simplified data that constitutes physically existing data and has many forms of existence, such as text data, image data, audio data, or a mixture of several types of data.

This article is shared by Huawei Cloud community “Java Knowledge point problem summary of raw data types”, originally written by breakDraw.

Java primitive data types are short, byte, int, long, Boolean, char, float, and double. Raw data is unprocessed or simplified data that constitutes physically existing data and has many forms of existence, such as text data, image data, audio data, or a mixture of several types of data. This is a summary of Java primitive data types.

Q: Does the size of data types in Java depend on whether the platform is 32-bit or 64-bit?

A: No, the VM is compatible with the platform

Q: When parsing data in Java, do we need to consider the size side of the processor? (i.e., is 12 of 0x1234 placed at high or low address)

A: No. Java shields the size side of the problem due to the virtual machine. You can query byteorder.nativeOrder () for details. In operating ByteBuffer, you can also set bytebuffer.order () :.

Q: How many bytes are short, int, and long in Java?

A: two, four, eight

Q: How many bytes are float, double?

A: Four, eight

Q: How many bytes are byte and char in Java? How many bytes is char in C++?

A: in Java, 1 and 2; in C++, char is 1

Q: The size of a Boolean type in Java?

A: Bool no space size (from Java programming ideas)

According to the docs.oracle.com/javase/tuto… Description of official documents:

boolean: The boolean data type has only two possible values:true and false. Use this data type for simple flags that track true/falseconditions. This data type represents one bit of information, But its “size” isn’t something that’s precisely defined.

Boolean types: Boolean data types have only two possible values: true and false. Use this data type as a simple token to track true/false conditions. This data type represents this information, but its “size” is not precisely defined.

Post a description of data types from the book:

Q: What are the immutable types?

A: Short, int, long, float, double, byte, char, Boolean, and String are immutable types. (This is just part of it, there are other immutable classes.)

Immutable type concept: as the contents of the values change, the corresponding memory address also changes.

Q: Are members of a class automatically initialized if they are primitive data types? Initialize to what?

A: Yes, initialize to 0 or false.

Q: In Java, local variables are not initialized.

A: will

Q: Can Boolean types be cast to other types?

A: No. Boolean b = 1 or Boolean b = “true” are not allowed

Q: When is implicit conversion not possible?

A: You can’t implicitly convert long to int or double to long if you lose precision. The compiler forces us to use a cast

Q: What is the type priority of the 8 primitive data types?

A :(byte/short/char)<int<long<float<double

Q: Which of the following is false?

A: Yes. Since two byte variables are automatically converted to ints when added, an error is reported when the int on the right is assigned to a short. (Cold knowledge)

Q:

Float f = 1.1;Copy the code

Didn’t you?

A: Float is followed by f. Plus f means float, otherwise double float.

Float f = 1.1 f; Double d1 = 1.1;Copy the code

Q: Can Boolean types be added, subtracted, multiplied and divided?

A: can’t

Q: Integer N= 0; int n = N; What happens at this point?

A: Automatic unpacking

Q: Comparison of integer packaging types, what output?

Integer num1 = 128,num2 = 128; System.out.println(num1==num2);Copy the code

A: Output false. If the value ranges from -128 to 127, Integer can be used to compare sizes directly with equals, but beyond that, equals does not work. The general reason is that in that range, Integer objects are directly used by cache objects, so the address is the same. Outside of that range, the Integer object will generate a new object, so the address is different.

One other note: “==” compares addresses for objects.

Q: What two Classes in Java can support integers with arbitrary precision and floating point numbers with arbitrary precision?

A: BigInteger and BigDecimal are also immutable classes.

Q: Do Java arrays have to be initialized manually?

A: No, array elements are automatically initialized to null or 0 or false.

Q: does Java support operator overloading in C++?

A: Not supported

Q: If (a=b)?

A: No, you can’t put an assignment in A conditional expression. Unless a and B are Boolean types.

Q: comparison of floating-point numbers

A: If it’s equal, it’s like this

if(Math.abs(a-b))<1E-6F)
Copy the code

If you use floating point a==b or a! If =b, exit from the while loop

Q: Which of the following array declarations are correct?

Char [] chr1 = new char[]{‘ A ‘, ‘B’, ‘C’};

Char [] chr2 = new char[3]{‘ A ‘, ‘B’, ‘C’};

C. char[][] chr3 = new char[][10];

D. char[][] chr4 = new char[10][];

E. char[] chr5 = new char[3];

A: ADE is right.

string

Q: The difference between StringBuffer and StringBuilder:

A: A StringBuffer is thread-safe, but A slow StringBuilder is thread-unsafe (that is, it can be read by multiple threads at the same time), but fast.

Q: String s = “123” + “456” + “789”; For static concatenation, it’s faster to concatenate a StringBuffer than a String, right?

A: No, after decomcompiling the code, we found that the code is String s = “123456789”; Because for static string concatenation, Java optimizes it completely at compile time, combining strings from multiple concatenation operations into a single long string at compile time. So note where StringBuffer/Builder is useful: a large number of concatenated strings in a for loop. Don’t blindly use StirngBuffer/Builder if it’s a splicing that the static compiler can sense

PS:

  • If you add string variables, it will optimize to StringBuilder for append

If you add constant strings, it will concatenate directly and you can check out this blog post, which shows the bytecode for both cases.

Q: What is the output next? Why is that?

String s1 = "Hello"; String s2 = "Hello"; String s3 = "Hel" + "lo"; String s4 = "Hel" + new String("lo"); String s5 = new String("Hello"); String s6 = s5.intern(); String s7 = "H"; String s8 = "ello"; String s9 = s7 + s8; System.out.println(s1 == s2); // trueSystem.out.println(s1 == s3); // trueSystem.out.println(s1 == s4); // falseSystem.out.println(s4 == s5); // falseSystem.out.println(s1 == s6); // trueSystem.out.println(s1 == s9); // falseCopy the code

Q: Println (s1 == s2) Output TruePrintln (s1 == s3) Output TruePrintln (S1 == S4) Output FalsePrintln (S4 == s5) Output FalsePrintln (s1 == s6) output Trueprintln (s1 == s9) prints false

  • The == operation on a string compares reference addresses.

  • If it’s a constant string written directly into the code, then the addresses are fixed, all in the constant pool.

  • Constant string concatenation that is written to death will still be put into the constant pool as constants. (Constant pool means the string is already known when the program is compiled.)

  • If String, the reference address is the address of the String object in the heap, and the address in the nonconstant pool. (Because the contents of a string are not always determined when the program is compiled, it cannot be put into the constant pool.)

  • So anything that involves string concatenation is not the same as the original constant. S7 and S8 are already strings, so they are not constant concatenations.

  • Intern will try to put the string into the constant pool.

Specific reasons can be seen

www.cnblogs.com/syp17265468…

Variable parameter

Q: How do I choose variable and fixed parameters when methods are overloaded? Output something like:

public static void main(String[] args) { f(1); } public static void f(int ... A){system.out.println (" + array.toString (a)); } public static void f(int a){system.out.println (" "+a); }Copy the code

A: Output fixed length parameter method.

Principle: if the overloaded method, the fixed parameter method can meet, the fixed parameter method is preferred, if not, then choose variable parameter method.

References:

www.cnblogs.com/syp17265468…

Click to follow, the first time to learn about Huawei cloud fresh technology ~