Java getting started and deepening

Source: Internet
Author: User
Java getting started and deepening-Linux general technology-Linux programming and kernel information. The following is a detailed description. 1. What are Java, Java2, and JDK? What is the 1.3 and 1.4.2 versions after JDK?

A: Java is a universal, concurrent, and strongly typed, object-oriented programming language (from the second edition of Java specifications). JDK is a free Java Development Tool distributed by Sun, the official name is J2SDK (Java2 Software Develop Kit ).

2 What is JRE/J2RE?

A: J2RE is Java2 Runtime Environment, that is, the Java Runtime Environment, sometimes referred to as JRE.

If you only need to run a Java program or Applet, download and install it.

If you want to develop Java software on your own, download JDK. J2RE is included in JDK.

Note: because Microsoft does not fully support Java, do not use the virtual machine that comes with IE to run the Applet. Be sure to install a J2RE or JDK.

3. What tools are better at learning Java?

A: The author suggests that you use the JDK + Text Editor first, which helps you understand the following basic concepts: path, classpath, package, and be familiar with basic commands: javac and java. Download the API help that is consistent with your JDK version.

If you are not sure about the usage of classes or functions, check the API instead of posting for help.

After you are familiar with Java, you can consider changing the IDE. Many people recommend JCreator. In fact, JCreator has weak functions.

The author recommended eclipse, download web site http://www.eclipse.org. Because eclispe is free of charge.

4. What are the good reference books for learning Java?

A: The author first recommends Thinking in Java, which is named "Java programming ideology" and has a Chinese version.

The first chapter of this book introduces many object-oriented programming ideas that should be carefully read by beginners.

In addition, the books of o'relly and Wrox are also good. The author himself does not like the books of Chinese Mainland authors.

Maybe you think English is too difficult, but most of the information on the Internet is in English. In addition, you need to check the API frequently, which is also in English.

5 Which of the following is better for Java and C ++?

A: This is an inappropriate question. You should ask: Which Java and C ++ are more suitable for my project?

If you do not need cross-platform or distributed systems, you need to emphasize the program running speed. C ++ is more suitable. What is the opposite? You should consider Java.

6. What is J2SE, J2EE, and j2s?

A: J2SE is a general Java.

It is intended for embedded devices, such as Java mobile phones. It has its own SDK. J2EE uses the J2SE SDK.

The J2EE specification is more about the requirements for J2EE servers and the constraints imposed by developers. For details, see the subsequent J2EE FAQ.

Ii. Commands

7. I wrote the first Java program. How should I compile/run it?

A: First, save the program as xxx. java file, and then use javac xxx in the dos window. java command, you will find that there is another xxx in this directory. class file, and then use the java xxx command to start running your java program.

8. I did as you said, but what "'javac' is not an internal or external command, or a program or batch file that can be run. ".

A: You have encountered a path problem. The operating system searches for javac.exe in a specific dimension (path.exe, but cannot be found. Edit the environment variable of your operating system, add a JAVA_HOME variable, set it to your JDK installation directory, edit the Path variable, and add % JAVA_HOME % \ bin. Close and open a new dos window, and you can use javac and java commands.

9 how to set environment variables?

A: Please contact someone else.

10 javac xxx. java passed smoothly, but what "NoClassDefFoundError" is displayed in java xxx "? Br>

A: You have encountered a classpath problem. The java command searches for the class file in a certain range (classpath), but cannot find it.

First, check that you have not mistakenly typed java xxx. class. Second, check your CLASSPATH environment variable. If you have set this variable and it does not contain. (representing the current directory? Br> Turbo? Br> you will encounter this problem. Please add one to your CLASSPATH environment variable. For more information, see section 15.

11. I displayed "Exception in thread" main "java. lang. NoSuchMethodError: main" in java xxx ".

A: First, each java file in your program has only one public class. The class name must be exactly the same as the file name. Second, there is only one public static void main (String [] args) method in the class you want to run. This method is your main program.

12 What does package mean? How to use it?

A: to uniquely identify and group each class, java uses the package concept.

Each class has a full name. For example, the full name of String is java. lang. String, where java. lang is the package name and String is the short name.

In this way, if you define a String, you can put it in mypackage and distinguish the two classes by using the full name mypackage. String and java. lang. String. At the same time, you can put the logic-related classes in the same package to make the program structure clearer. All you need to do is add a line "package mypackage;" at the beginning of the java file ;".

Note that the packages are not nested or contain relationships. Packages A and A. B are two parallel packages for java commands.

13. What if I didn't declare any package?

A: Your class is considered to be in the default package. The full and short names are the same.

14. How to use other classes in one class?

A: If you use classes in the java. lang package, you do not have to do anything.

If you use classes in other packages, use import package1.class1; or import package2. *; here. * Indicates introducing all classes in this package. Then you can use the short names of other classes in the program. If short names conflict, use the full name to differentiate them.

15 "NoClassDefFoundError" is displayed when a package is used, but it works normally when I remove all packages.

A: store your java files by package name.

For example, if your working directory is/work and your class is package1.class1, store it as/work/package1/class1.java. If no package is declared, put it directly under/work.

Run javac package1/class1.java under/work and then java package1.class1. You will find everything works. In addition, you can consider using IDE.

16 What should I do if I want to compile java into an exe file?

A: JDK can only compile java source files into class files.

A class file is a cross-platform bytecode that must be run based on the platform-related JRE. Java to implement cross-platform. Some development tools can compile java files into exe files. The author objected to this practice because cross-platform is canceled. If you are sure that your software only runs on Windows, you can use C ++/C # for programming.

17 what does "deprecated API" I encountered during compilation mean?

A: What does deprecated mean? ?? Br> these methods may be removed later. You should use a newer method. Generally, the API describes how to replace it.

Iii. I/O

18 how do I add startup parameters to a java program, just like dir/p/w?

A: Do you still remember public static void main (String [] args? Here, args is your startup parameter. When you enter java package1.class1-arg1-arg2 at runtime, there will be two strings in args, one is arg1 and the other is arg2.

19 How do I input an int/double/string from the keyboard?

A: java I/O operations are a little more complex than C ++ operations. If you want to input data from the keyboard, the sample code is as follows: BufferedReader cin = new BufferedReader (new InputStreamReader (System. in); String s = cin. readLine ();

In this way, you get a string. If you need a number, add: int n = Integer. parseInt (s); or double d = Double. parseDouble (s );

20 how can I output an int/double/string?

A: start writing in the program:

PrintWriter cout = new PrintWriter (System. out );

Write as needed:

Cout. print (n); or cout. println ("hello.

21 I found that some books directly use System. in And System. out input and output, which is much simpler than you.

A: java uses unicode, which is a double byte. System. in And System. out are single-byte streams.

If you want to input and output double-byte text such as Chinese, use the author's practice.

22. How do I input an int/double/string from a file?

A: It is similar to inputting data from the keyboard, but it is changed to BufferedReader fin = new BufferedReader (new FileReader ("myFileName "));

PrintWriter fout = new PrintWriter (new FileWriter ("myFileName "));

If you have not downloaded the API, download it and read the content in the java. io package.

23 What should I do if I want to read and write the specified file location?

A: You certainly didn't take the API seriously. Java. io. RandomAccessFile can meet your needs.

24. How can I judge that the object to be read has reached the end?

A: You certainly didn't take the API seriously. In the Reaer's read method, the returned-1 indicates the end of the stream.

4. Keyword

25 how to define Macros in java?

A: java does not support macros, because the replacement of macros cannot guarantee type security.

To define a constant, you can define it as a static final member of a class. See 26 and 30.

26 const cannot be used in java.

A: You can use the final keyword. For example, final int m = 9. Variables declared as final cannot be assigned another value. final can also be used to declare methods or classes. Methods or classes declared as final cannot be inherited. Note that const is a reserved word of java for expansion.

27 goto cannot be used in java.

A: You do not need to use goto in process-oriented languages. Check whether your program process is reasonable.

If you need to quickly jump out of multi-tier loops, java enhances the break and continue features (compared with C ++.

For example:

Outer:

While (...)

{

Inner:

For (...)

{

... Break inner ;...

... Continue outer ;...

}

}

Like const, goto is a reserved word of java for expansion.

28 Can I overload operators in java?

A: No. The + sign of String is the only built-in overload operator. You can implement similar functions by defining interfaces and methods.

29. I have a new object, but cannot delete it.

A: java has an automatic memory reclaim mechanism, namely, Garbarge Collector. You no longer have to worry about pointer errors.

30. I want to know why the main method must be declared as public static?

A: It is declared as public so that this method can be called externally. For details, see article 37 of object.

Static is used to associate a member variable/method to a class rather than an instance ).

You can directly use the static members of this class without creating an object. Calling static members of Class B in Class A can be written in B. staticMember.

Note that the static member variables of a class are unique and shared by all class objects.
Related Article

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.