CoreJava

?what is java applets :java programs that work on the web pages are called applets (applets is the abbreviate of “applications”).to use a applets you only need a Java-enabled web browser, you need not installed any software .you can get the latest version of the program whenever you you visit the web page containing the applet.the applets can change when you command it, and can exchange the data between the computer presenting the applet and the computer serving it.But today not too much browser use java applets to get the dynamic effect ,most of them use JavaScript and Flash when dynamic effects are desired in the browser. Java,on the other hand ,has become the most popular language for developing the server-side applications that produce web page and carry out the backend logic.
?when you use terminal to run a java program ,you should use “cd” to change to the directory which your java project is saved.if you do not change to this directory ,the compile will succeed ,but the interpretation will not succeed(it means you can use “javac XXX.java”to compile and get “XXX.class”but you can not use”java XXX” to let it run ,it will notice that “can not find the main class ””ClassNOTFind”).
?when you use
/**

*/
To comment ,the text in it will generate documentation automatically.
?there is a subtle difference between the “println”method and the “sqrt” method .the “println”method operates on an object ,”System.out” define in the “System”class.but the “sqrt” method in the “Math” class does. Not operate on any object.Such a method is call a “static” method.
?Substring:There is one advantage to the way “substring”works :computing the length of the substring is easy.The string “s.substring(a,b)”always has the length b-a.
?Empty and Null String:
You can test whether a String is empty by use
“if(str.length()0)”. Or
“If(str.equals(“”))”
You can test whether a String is Null by use
“if(str
null)”
Sometimes you need to test that a String is neither “null” nor “empty”,you can use
“if(str!=null&&str.length()!=0)”
You should test that a str is not “null” at first ,because it is important that it is an error to invoke a method on the “null”value.
?reading input:eg.
package cn.My;

import java.util.Scanner;

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

	//get the first input"name"
	System.out.println("what is your name?");
	String name=in.nextLine();
	
	//get the second input "old"
	System.out.println("how old are you?");
	int age=in.nextInt();
	
	//display output on the console
	System.out.println("Hello "+name+".Next year ,you will be "+(age+1));
	
}

}

✌️what is your name?
elaine
how old are you?
20
Helloelaine.Next year ,you will be 21

?some tips:
public class Hi {
public static void main(String[] args) {
int n;

{
int k;


}//k is only defined up to here
}
}

public class Hi {
public static void main(String[] args) {
int n;
{
int k;
int n;//error— can not redefine n in the inner block in java,
//but you can do it in c++
}
}
}

?for:
@1.public class Hi {
public static void main(String[] args) {
for(int i=1;i<=10;i++) {

}//i no longer defined here
}
}
@2.if. you want “I”can also be use out of the block
public static void main(String[] args) {
int i;
for(i=1;i<=10;i++) {

}//i is still define here
}

@3.you can define “I”in two block ,but the “I”in different block will not influence the other
public static void main(String[] args) {

for(int i=1;i<=10;i++) {
	
	}
for(int i=11;i<=20;i++) {
	
	}//ok to define other variable named "i"
}

?tips:there is an even easier way to print all values of the array, using the “toString” method of the “Arrays”class.the call “Arrays.toString(a)”returns a string containing the array elements, enclosed in brackets and separated by commas.So to print a array ,simply call “System.out.println(Arrays.toString(a));”
?tips:it is legal to have arrays of length 0.
?“For example, ‘A’ is a character constant with value 65. It is different from “A”, a string containing a single character. ”
?“if (x = 0) // oops… meant x == 0
In C++, this test compiles and runs, always evaluating to false. In Java, the test does not compile because the integer expression x = 0 cannot be converted to a boolean value.”
?“The difference between the two only appears when they are used inside expressions. The prefix form does the addition first; the postfix form evaluates to the old value of the variable.
int m = 7;
int n = 7;
int a = 2 * ++m; // now a is 16, m is 8
int b = 2 * n++; // now b is 14, n is 8
?“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. Sure, if strings are in the same location, they must be equal. But it is entirely possible to store multiple copies of identical strings in different places.
“String greeting = “Hello”; //initialize greeting to a string
if (greeting == “Hello”) . . .
   // probably true
if (greeting.substring(0, 3) == “Hel”) . . .
   // probably false
If the virtual machine always arranges for equal strings to be shared, then you could use the == operator for testing equality. But only string constants are shared, not strings that are the result of operations like + or substring. Therefore, never use == to compare strings lest you end up with a program with the worst kind of bug—an intermittent one that seems to occur randomly.”

?“if (greeting.compareTo(“Hello”) == 0) . . .
but it seems clearer to use equals instead.

?“ Empty and Null Strings
The empty string “” is a string of length 0. You can test whether a string is empty by calling
if (str.length() == 0)
or
if (str.equals(""))
An empty string is a Java object which holds the string length (namely 0) and an empty contents. However, a String variable can also hold a special value, called null, that indicates that no object is currently associated with the variable. (See Chapter 4 for more information about null.) To test whether a string is null, use the condition
if (str == null)
Sometimes, you need to test that a string is neither null nor empty. Then use the condition
if (str != null && str.length() != 0)
You need to test that str is not null first. As you will see in Chapter 4, it is an error to invoke a method on a null value.”
?多态
BaseClass ploy=new SubClass();
引用父类BaseClass的类型,但使用子类SubClass实例对象的方法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值