欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

Java: reading Core Java 2,

来源: javaer 分享于  点击 13447 次 点评:246

Java: reading Core Java 2,


这个暑假的小学期学Java和汇编。


 public class ClassName
{
     
public static void main(String[] args)
   
{
        program statements
    }

}
 
Chapter 3 Fundamental Programming Structures in Java

You need to make the file name for the source code the same as the name of the public class, with the extension .java appended.

System.out.println("Hello world!");

Comparison between C/C++ and Java
The same:
1. Case sensitive
2. The first letter of a class or fuction or variable must be a letter. Digits are not allowed.
3. Strongly typed language
4. Use // and /* */ to mark the comments.


The different
1.Everything in a Java program must be inside a class
2. /** */ is another kind of comment, which can be used to generate documentation automatically.
3. Use final to denote a constant in Java instead of const

Data Type

Java Integer Types
Type
Storage Requirement

int
4 bytes
short
2 bytes
long
8 bytes
byte
1 byte

Floating-Point Types
The char Type

Comparison between C/C++ and Java
The same:
1. The data-type are the same generally.

The different
1.The char type in Java uses Unicode encoding scheme, while in C/C++ it uses ASCII.
2. boolean in Java == bool in C/C++
3. Initializing Variables before use is a must in Java

Operator

Comparison between C/C++ and Java

The differences:
1. You can not cast between boolean values and any numeric type. This convention prevents common errors.
In the rare case that you want to convert a boolean  value to a number, you can use  a conditional expression
such as b ? 1 : 0.
The same:
1. They are the same generally.

Strings
Java strings are implemented as sequences of char values. ...

Code Points and Code Units
The length method yields the number of code units required for a given string in the UTF-16 encoding. For example:
String greeting = "Hello";
int n = greeting.length();//is 5
To get the true length, that is, the number of code points, call
int cpCount = greeting.codePointCount(0, greeting.lengrh()); The call s.charAt(n) returns the code unit at position n, where n is between 0 and s.length()-1.
For example,
char first = greeting.charAt(0);//first is 'H'
char last = greeting.charAt(4);// last is 'o' to get at  the ith code point, use the statements
int index = greeting.offsetByCodePoints(0,i);
int cp = greeting.codePointAt(index)
To extracta substring from a larger string, use the following statements
String greeting = "Hello";
String s 
= greeting.substring(0,3);//s == "Hel" Note that s.substring(a,b) always has b-a code units. Because s.substring(a,b) will copy the code units from position a to b-1.

Testing Strings for Equality
Do not use  the == operator to test whether two strings are equal! It only determines whether or not the strings are stored in the same
location. To check whether string s and t are equal, use s.equals(t) instead.

The different from  C/C++
1. UTF-16
2.Code Unit and Code Point
3.Immutable
4.Testing Strings for Equality

Input and output
import java.util.*;//The Scanner class is defined in java.util package

public class InputTest
...{
     
public static void main(String[] args)
     
...{
           Scanner in 
= new Scanner(System.in);

           
// ger first input
           System.out.print("What is your name?");
           String name 
= in.nextLine();

           
// get second input
           System.out.print("How old are you ?");
           
int age = in.nextInt();
           
           
//display output on console
           System.out.println()"Hello, " + name + ". Next year, you'll be " + (age +1) ;
     }

}

相关文章

    暂无相关文章
相关栏目:

用户点评