Java Basics--java Getting started and deepening __java

Source: Internet
Author: User
Tags deprecated goto reserved
1 What is Java, Java2, JDK? What about the 1.3, 1.4.2 version number behind the JDK?

A: Java is a generic, concurrent, strongly typed, object-oriented programming language (excerpted from the Java Specification Second Edition) JDK is a free Java development tool distributed by Sun Company, officially named J2SDK (JAVA2 Software Develop kit).

2 What is Jre/j2re?

A: J2re is JAVA2 Runtime environment, 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 your own Java software, please download the JDK. J2re is included with the JDK.

Note Because Microsoft's support for Java is incomplete, do not use IE's own virtual machine to run applets, and be sure to install a j2re or JDK.

3 What tools do you use to learn Java better?

A: The authors recommend that you use the jdk+ text editor first, which helps you understand the following basic concepts: Path,classpath,package and familiarity with basic commands: Javac and Java. and download the same API help as your JDK version.

If you are unsure of the use of a class or function, consult the API first instead of posting for help.

When you are familiar with Java, you can consider changing an IDE. A lot of people recommend JCreator, actually the function of JCreator is still very weak.

The author recommends eclipse, download URL http://www.eclipse.org. Because the Eclispe is free.

4 Learning Java What are the good reference books?

A: The author first recommended thinking in Java, Chinese name "Java programming thought", with Chinese version.

The first chapter of the book introduced a lot of object-oriented programming ideas, as a novice should read carefully.

In addition, the books of O′relly Press and Wrox Press are also good. The author does not like the books of the 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 Java and C + + which is better?

A: This question is a very inappropriate question. You should ask: Java and C + + which is more suitable for my project?

If you do not need to cross the platform, do not need to distribute, to emphasize the speed of the program, C + + more applicable. Instead, you should consider Java.

6 What is J2SE/J2EE/J2ME?

A: J2SE is the general Java.

J2ME is for embedded devices, such as the Java phone, which has its own SDK. And the Java EE uses the J2SE SDK.

The Java EE specification is more about the requirements of Java server and the constraints of developers. For details, see the successor, "Java EE FAQ."

Ii. Order Articles

7 I wrote the first Java program, how should I compile/run?

A: First, please save the program as a Xxx.java file, and then use the Javac Xxx.java command under the DOS window, you will find that the directory more than a Xxx.class file, and then use the Java XXX command, your Java program began to run.

8 I did what you said, but what happened? "′javac′ is not an internal or external command, it is not a running program or a batch file. "。

A: You're having a path problem. The operating system searches for javac.exe within a certain range (path), but cannot be found. Please edit your operating system environment variable, add a Java_home variable, set the installation directory for your JDK, edit the path variable, and add a%java_home%/bin. Then turn off and open a new DOS window, and you can use the Javac and Java commands.

9 environment variable How to set?

A: Please consult the people around you.

Ten Javac Xxx.java smoothly passed, but the Java XXX show what "Noclassdeffounderror"?br>

A: You're having a classpath problem. The Java command searches for the class file you want to use within a certain range (classpath), but cannot be found.

First make sure you are not wrong to knock into Java Xxx.class, and secondly, check your CLASSPATH environment variables if you set the variable and do not contain it. ?br> Rui?br> (representing the current directory) you're going to have this problem. Please add an item to your CLASSPATH environment variable. See in addition 15.

11 I show "Exception in Thread" main "Java.lang.NoSuchMethodError:main" when I am in Java XXX.

A: First of all, in your program each Java file has and can only have a public class, the class name must be the same as the file name of the exact same case. Second, there is and only one public static void main (string[] args method in the class you want to run, and this is your main program.

What do you mean by package?

A: In order to uniquely identify each class and group, Java uses the concept of package.

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.

So, if you also define a string, you can put it in mypackage by using the full name MyPackage. String and java.lang.String to differentiate between these two classes. At the same time, placing logically related classes in the same package can make the program structure clearer. All you have to do is add a line "package MyPackage" to the beginning of the Java file.

Note that packages are not nested or contain relationships, and a and a.b packages are two packages in parallel to the Java command.

13 I did not declare any package what would happen?

A: Your class is considered to be placed in the default package. The full name and the short name are consistent.

14 How to use other classes in a class?

A: If you use the class in the Java.lang package, you don't have to do anything.

If you use the class in the other package, use the import package1.class1; or import package2.*; * Represents the introduction of all the classes in this package. Then you can use the short name of the other class in the program. If the short name is conflicting, use the full name to differentiate it.

15 I used the package time to show "Noclassdeffounderror", but I put all package removed when the normal operation.

A: Store your Java files by the name of the package.

For example, your working directory is/work, your class is Package1.class1, then store it as/work/package1/class1.java. If the package is not declared, put it directly under/work.

Execute Javac Package1/class1.java under/work, and then execute Java package1.class1, and you'll find everything's fine. Alternatively, you can consider starting with the IDE.

16 I want to compile Java into EXE file, what should I do?

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

A class file is a Cross-platform byte code that must be run by a platform-dependent JRE. Java to achieve cross-platform. Some development tools can compile Java files into EXE files. The author rejects this practice because it cancels the cross-platform nature. If you are sure that your software is running on only the Windows platform, you can consider using c++/c# to program.

17 What do I encounter when compiling the "deprecated API"?

A: The so-called deprecated refers to the already, but for the sake of forward compatibility still retain the method?? Br> These methods may cancel support later. You should switch to a newer method. Generally in the API will indicate what method you should use to replace it.

Third, I/O article

18 How do I add a startup parameter to a Java program, like dir/p/w?

A: Remember the public static void main (string[] args)? The args here is your startup parameter. At runtime you enter the Java Package1.class1-arg1-arg2,args with two strings, one is arg1 and the other is arg2.

19 How do I enter a int/double/string from the keyboard?

A: Java I/O operations are more complex than C + +. If you want to enter from the keyboard, the sample code is as follows: BufferedReader cin = new BufferedReader (new InputStreamReader (system.in)); String s = cin.readline ();

So you get a string, if you need a number, plus: int n = integer.parseint (s); or double D = double.parsedouble (s);

20 How do I output a int/double/string?

A: At the beginning of the program writing:

PrintWriter cout = new PrintWriter (System.out);

Write when required:

Cout.print (n); or cout.println ("Hello") and so on.

21 I've found that some books use system.in and System.out to enter and output directly, much simpler than you.

A: Java uses Unicode, which is double-byte. and system.in and System.out are single-byte stream.

If you want to enter the output of double-byte text such as Chinese, please use the author's approach.

22 How do I enter a int/double/string from the file?

A: Similar to the input from the keyboard, just change to bufferedreader fin = new BufferedReader (New FileReader ("myFileName"));

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

In addition, if you have not yet downloaded the API, please start downloading and reading the contents of the Java.io package.

23 I want to read and write files in the specified location, what should I do?

A: You certainly didn't look at the API carefully. Java.io.RandomAccessFile can meet your needs.

24 How to judge that the document to be read has reached its end?

A: You certainly didn't look at the API carefully. In the Reaer read method, it is explicitly stated that return-1 represents the end of the stream.

Iv. Key Words and articles

How do you define macros inside Java?

A: Java does not support macros because macro substitution does not guarantee type safety.

If you need to define a constant, you can define it as a static final member of a class. See also 26 and 30.

In Java there is no const.

Answer: You can use the final keyword. For example final int m = 9. A variable declared final cannot be assigned again. Final can also be used to declare a method or class, and a method or class declared final cannot be inherited. Note that const is a reserved word for Java to expand.

You can't use Goto in Java either.

A: Even in a process-oriented language, you can use no goto at all. Please check that your program flow is reasonable.

If you need to jump out of a multi-layer loop, Java enhances the break and continue functions (compared to C + +).

For example:

Outer:

while (...)

{

Inner:

For (...)

{

... break inner; ...

... continue outer; ...

}

}

As with const, Goto is also a reserved word for Java to expand.

Can you overload the operator in Java?

Answer: No. The + number of the string is the only built-in overloaded operator. You can implement similar functions by defining interfaces and methods.

29 I have a new object, but I can't delete it.

A: Java has automatic memory recovery mechanism, that is, the so-called Garbarge Collector. You never have to worry about pointers being wrong.

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

A: The declaration is public for this method can be called externally, and the details are met to object article 37.

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

You don't have to create an object to use the static members of the class directly, and the static members of Class B in Class A can use B.staticmember.

Note that the static member variable of a class is unique and is shared by all of the class objects.

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.