Hello everyone, I am brother node, known as Brother Qing in the world. I recently read the very popular book "Java from Getting Started to Being Buried" I'm very fascinated. I heard that it ranks first in the sales rankings of major bookstores? It seems that there are still many friends who love to learn! They say knowledge changes destiny and Java makes life, so it seems that my decision to learn Java was the right one!
But having said that, this book is really well written, but after all, I have just started, and I am still a little confused about some small knowledge points, such as the data types I just learned. , which is still a bit difficult for me with zero foundation.
On this day, I was bored and wandering around on CSDN and saw a blog: [Data Structure and Algorithm 05, Red-Black Tree After reading 包Understand~ 】. To be honest, I am very disdainful when I see articles like this. Red-black trees? What tree is it? Why is it half red and half black? Do you understand after reading this? What if I understand half of it? Isn't that a dystocia? snort! Let me show you what kind of monster you are.
With a little tap of my hand, I came in. I was very disapproving at first, but the further down the line, the more surprised I became... Two minutes later, I almost knelt down! This is so well written. I feel like it’s more detailed than what’s in the book (although I didn’t understand it)! Look at this full of useful information, this fresh and refined text, this full screen of [knowledge], how could I bear to miss it? Why don't you let him quickly go to my favorites to eat ashes? Watch me follow, like, and add to favorites, one after another, to make everything clear for the boss.
With excitement, I quietly clicked on the boss’s dialog box.
I: Boss, are you there?
After a while, the boss actually replied!
武哥: What’s the matter~?
Me: (I don’t know how to organize the words) Um... Boss... you are so handsome! So cool!
武哥: Harmful! Isn't this accepted?
我: (With a black line on his forehead, I bet this guy is a bit narcissistic. I’m just trying to flatter him for business, but he’s quite proud of himself?) Haha~ Boss, that’s it, I am a newbie learning Java, can you help me?
武哥: Well...this...is also ok, I charge a fee
Me: (I thought I was indeed a big boss, and I still charge a fee) Big brother, I like Java very much. I recently bought a book to study. The title is "Java from Getting Started to Being Buried". I think this book is very good, but I have a little doubt and would like to ask some questions about learning. Do you think there can be a back door?
Brother Wu: Then... okay. Since you are so eager to learn, I will give you some pointers. Where have you learned now?
我: I just learned the basics of Java, and I’m a little confused when I see the data structure~
武哥: That’s easy to say , it took me more than half a year to learn the basics of Java! The foundation is very solid. I can take you through this knowledge point together.
I: Really? Boss! Thank you so much! I'll treat you to dinner later!
武哥: Small problem, small problem. I see you are in Hefei too?
我: Yes~ (A little panicked, quietly clicked on the boss’s profile, could it be...)
武哥: Haha, What a coincidence! I'm here too!
I: Aha? Such a coincidence? How about... (It seems like a meal can't escape, I cry...)
武哥: You add me on WeChat, I will send you a location, let's talk in detail .
I: (Is this...a face-to-face encounter? Only a few minutes of communication? Are all the big guys so coquettish? Although I am not willing in my heart, my body is very honest) emm... Okay, I'll be there right away...
Brother Wu: Okay, I'll wait for you~
I was trembling...there was a trace of panic in my heart!
Half an hour later, I took a taxi according to the address given by Brother Wu.
When I saw Brother Wu, as expected, he was extremely angry!
I saw Brother Wu slicking his hair back, applying some oil, leaning on the car at an angle of 45 degrees, looking up at the sky, lighting a cigarette, and feeling endless vicissitudes of life and loneliness.
I: (I bite the bullet and go up to say hello) Brother Wu?
武哥: (stopped smoking and glanced at me, his eyes a little confused): Hello? Are you a node brother?
我: Well...yes (quietly takes a few steps back)
武哥: (Strides over and puts his arm around my shoulders) Let's go to a coffee shop, have a cup of coffee, and talk about Java. How about that?
Me: (I thought you were like this, but I still refused!) Okay...
So, Brother Wu hugged me and twisted. I went to Starbucks...and found a quiet private room...
I guess you can guess the next scene...
Ahem! Be serious! We are here to discuss learning!
What? You do not believe?
Then take a look below, Dry information warning!
Java data There are many types. This article mainly summarizes them from four aspects: basic types, packaging types, reference types and cache pools.
The basic data types include byte, short, int, long, float, double, boolean, and char. Regarding their classification, I drew a picture.
Next, I will summarize it into a table from the aspects of byte count, data range, default value, and purpose, so that it is clear at a glance .
The basic types mentioned above have corresponding packaging types, for the convenience of readers , I also compiled a table.
In Java, reference type variables are very similar to C/C pointers. A reference type points to an object, and a variable pointing to an object is a reference variable. These variables are assigned a specific type when declared, such as Student, Dog, etc. Once a variable is declared, its type cannot be changed.
Objects and arrays are reference data types. The default value for all reference types is null. A reference variable can be used to refer to any compatible type. For example:
Dog dog = new Dog("旺财")。
How to convert between packaging types and basic types?
Integer x = 2; // 装箱 调用了 Integer.valueOf(2) int y = x; // 拆箱 调用了 X.intValue()
How to convert between basic types? There are two points:
Forced type conversion must be used when converting a large-capacity type to a small-capacity type.
# Converting small-capacity types to large-capacity types can be automatically converted.
For example:
int i =128; byte b = (byte)i; long c = i;
Let’s think about a question: new What is the difference between Integer(123)
and Integer.valueOf(123)
?
Some people may know, some may not. In fact, they are very different.
new Integer(123) 每次都会新建一个对象;
Integer.valueOf(123) 会使用缓存池中的对象,多次调用会取得同一个对象的引用。
我写个demo大家就知道了
Integer x = new Integer(123); Integer y = new Integer(123); System.out.println(x == y); // false Integer z = Integer.valueOf(123); Integer k = Integer.valueOf(123); System.out.println(z == k); // true
编译器会在自动装箱过程调用valueOf()
方法,因此多个值相同且值在缓存池范围内的 Integer 实例使用自动装箱来创建,那么就会引用相同的对象。如:
Integer m = 123; Integer n = 123; System.out.println(m == n); // true
valueOf()
方法的实现比较简单,就是先判断值是否在缓存池中,如果在的话就直接返回缓存池的内容。我们看下源码就知道。
public static Integer valueOf(int i) { if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i); }
根据数据类型的不一样,这个缓存池的上下限也不同,比如这个 Integer,就是 -128~127。不过这个上界是可调的,在启动 jvm 的时候,通过 -XX:AutoBoxCacheMax=
参考自StackOverflow:
https://stackoverflow.com/questions/9030817/differences-between-new-integer123-integer-valueof123-and-just-123
OK,关于Java数据类型的小知识就分享到这了,虽然我还有点意犹未尽…
Java 的数据类型虽然简单,但是里面还是有很多小细节值得我们玩味的,希望这篇文章能给大家带来一些帮助。
The above is the detailed content of Haven’t learned Java data types? Because the posture is useless!. For more information, please follow other related articles on the PHP Chinese website!