JAVA Learning Notes

Source: Internet
Author: User

The first knowledge of Java is at the end of a freshman year, mainly from Mu class to learn the basic grammar of Java. Actually feel big may not need, big a semester in the data structure class to the C language deep study, actually already has the C language the grammatical structure to understand thoroughly, the data structure also has the deeper understanding. Java is syntactically similar to C language, and its core is object-oriented.

What I remember most about Java is a variety of ways. This allows me to focus more on what the program is going to accomplish without having to spend too much time obsessing over the basic data structure.

In the data structure course, we spend a lot of time figuring out how to implement various data structures. Take the picture, we focus on the binary tree. How the binary tree is generated, how it is traversed, how it is inserted, these basic but cumbersome code is our own line of knocking out, but Java's rich and diverse inside for us to build a lot of infrastructure data structure implementation code time, so that we can focus more on the project rather than the program. Still take the picture, although only touches the hashmap, but I already for the Java method's enrichment deeply shocked.

Building a hash is simple:

1 New Hashmap<string, integer> ();

Here we create a key value of type string, and the value is hash of type int.

For the operation of Hash, Java provides a variety of methods, my own most commonly used is the following:

1 this. Map.containskey (key)            //< contains key>2this. Map.get (key)                        //< get key value>3this. Map.put (key,value)               // < Insert data > 4 this. Map.replace (key,value)         //< replace data >

Then there is the sort of hashmap, which, in general, stores the sorted structure into the ArrayList, implementing the code as follows:

1  PublicArraylist<entry<string, integer>> sortmap (hashmap<string, integer>Oldmap) {2arraylist<entry<string, integer>> list =NewArraylist<entry<string, integer>>(Oldmap.entryset ());3Collections.sort (list,NewComparator<map.entry<string, integer>>(){4              Public intCompare (map.entry<string, integer> O1, map.entry<string, integer>O2) {5                 return(O2.getvalue ()-O1.getvalue ());6             }7         });8         returnlist;9}

Here is to say is HashMap entry, represents the data entity composed of <Key,Value>, want to use it, cannot simply do this

1 import Java.util.Map;

And to import this:

1 import java.util.Map.Entry;

This allows you to use entry, which is the order in which the ordering is implemented:

1 build ArrayList (via entry) and  sort ArrayList using the collection method (implemented by a custom comparator comparer)

It is important to note that there is no point in ordering the HashMap itself, we can only get the sorting result and not change the data structure of the HashMap itself.

Feeling the above statement is a bit cumbersome, but the self-feeling hashmap This data structure is fast and powerful, and Java comes with a way to let it.

Well, I should speak in detail about my own gains and feelings in terms of object-oriented.

1th: Do not set the data in class to public anyway, this is the object-oriented taboo.

Setting to public means that users can modify the data of your program at will, which is dangerous and unacceptable, and the teacher illustrates it vividly with an example.

For example, we now construct a very simple class getvolumn, where the user inputs are long, wide, and high, and this class calculates the volume of the box.

Well, to achieve this, we need to create a volumn, right?

1  Public double volumn;

That way, it's broken. We do:

1 new Getvolumn ();  // a,b,c for length, width, height, respectively 2 myvol.a = +; 3 myvol.b = 1; 4 myvol.c = 2; 5 myvol.calculate (); 6 myvol.vol =; 7 System.out.println ("The Volumn is:", Myvol.vol);

What is the result of this output? Obviously 300 and the original result 200 do not match at all. So it's dangerous for users to be able to modify data directly, and what we should do is encapsulate the data.

The simplest package, of course, is to construct the so-called setter () and Getter () methods, and of course, this is to let you understand the encapsulation principle, when actually writing the program, as long as it satisfies:

1 Private/protected data type variable name;

is basically OK, and then modifies the value of the variable through the various methods of the class. Its essence is to restrict the user's behavior to the program, so that the user can only obtain or modify the data through the method prescribed by the program, and greatly improve the security of the program.

2nd, inheritance

Why to inherit? Inheritance can be seen as an extension of class functionality. On the basis of inheriting the original class, further expansion. This effect is to allow a class to focus more on the implementation of this class of functions, so that the class is not too tired, so as to improve the efficiency of the program.

The implementation of inheritance is as follows:

1  Public class extends inherited class {2      Method 1;3      Method 2;   4 }

After inheritance, you need to be aware of the construction method:

1. No special action is required for the constructor of the inherited class without parameters (in fact, the compiler automatically generates super ());2. For the constructor of the inherited class, you need to construct the method's < first line > call in this class :  Super(parameter 1, parameter 2, parameter 3,) ;

After inheritance, this class can directly use the methods and protected data in the inherited class.

The inheritance aspect actually as a beginner I know and use not much, but it's let a class more focus not too busy to do all the thought really let me good.

3rd, Polymorphic, said the truth, polymorphism although understand, but I have not used once, the level is limited, hey.

4th, interface.

How do I create an interface?

This allows the interface to be created, and the interface is focused on what methods the program should implement, rather than how to implement these methods, it can be said that the interface is a specification.

To a small Amway:

You can see that the interface does not have to do with the implementation method, even {} is thrown, for the method specified in the interface can be implemented as follows:

As you can see, after instantiation, directly using the method in the interface is OK, if more than one class implements the interface method, the interface is instantiated directly, so that the code will be more concise, and this is also a manifestation of polymorphism.

Here, the object-oriented sentiment is over, actually I prefer Java's colorful library and various methods than object-oriented.

After putting into the embrace of Java, the use of Java not only improve the speed of writing programs, but also improve the quality of the program, there are object-oriented specifications, and by constructing different classes to achieve different functions, as well as the Java integrated data structure, so that the code more concise and efficient.

As a beginner, my feelings are these, not many, but I think it is my personal code ability to improve a milestone it.

After all, there will be a variety of learning content to deal with, but also to write a variety of functions of the program, but a word:

JAVA, make it easier to write a program!

JAVA Learning Notes

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.