java语言的特征
基础类型
代码展示如下:
// 字节类型 最大值127
byte b = 127;
System.out.print(b);
System.out.println();
// short 最大值2的15次方-1
short s = 9999;
System.out.println(s);
// int 最大值2的31次方-1
int i = 9999999;
System.out.println(i);
// long 最大值为2的63次方-1
long l = 6666666;
System.out.println(l);
// char
char c = 'a';
System.out.println(c);
// boolean
boolean flag = false;
System.out.println(flag);
// flaot
float fff = 333.68888f;
float fff2 = 555.777F;
System.out.println(fff);
System.out.println(fff2);
// double
double dd = 666.888;
System.out.println(dd);
数组
// 直接初始化
String[] aArray = new String[5];String[] bArray = { "a", "b", "c", "d", "e" };
String[] cArray = new String[] { "a", "b", "c", "d", "e" };
String[] stringArray = { "a", "b", "c", "d", "e"};
System.out.println(aArray);System.out.println(bArray);// 实例化一个数字列表ArrayListarrayList = new ArrayList(Arrays.asList(stringArray));
System.out.println(arrayList);
set集合
Set set = new HashSet();
set.add("婴儿");
set.add("儿童");
set.add("少年");
set.add("成年");
set.add("儿童");// 重复的set会将其自动去掉
System.out.println(set.size());
System.out.println(set);
Iterator iterator = set.iterator();
while (iterator.hasNext()) {
Object ii = iterator.next();
System.out.println(ii);
if ("少年".equals(ii)) {
System.out.println("就说了存在" + ii + "的吧,这下对了吧!");
}
}
list集合
List list = new ArrayList();
list.add("语文");
list.add("数学");
list.add("英语");
list.add("语文");
System.out.println(list.size());
System.out.println(list);
// for循环
for (int i = 0; i < list.size(); i++) {//++i
// get方法
System.out.println(list.get(i));
}
map集合
HashMap hashmap = new HashMap();
hashmap.put("Item0", "Value0");
hashmap.put("Item1", "Value1");
hashmap.put("Item2", "Value2");
hashmap.put("Item3", "Value3");
Set set = hashmap.entrySet();
// 迭代器
Iterator iterator = set.iterator();
do {
Map.Entry mapentry = (Map.Entry) iterator.next();
System.out.println(mapentry.getValue()); // 获取给定key对应的值
System.out.println(mapentry.getKey());// 获取给定key
} while (iterator.hasNext());
类
public class TestClassDemo {
//公共
static int PORT = 8080;
//私有
private static String IP = "192.168.11.3";
public static boolean test = true;
protected static final String MYNAME="liaidong"; //定义中用了final,就是不能改变指的。
public TestClassDemo(String ip){
super();
this.IP=ip;
}
//公共方法
public boolean getData(){
String databaseName = "eayun";//内部变量
int ii = 100;
System.out.println(databaseName+this.IP);//this.IP调用的内部全局变量
return this.test;
}
//默认方法
boolean getrandom(){
System.out.println(Math.random());// 结果是个double类型的值,区间为[0.0,1.0)
return this.test;
}
//私有方法
/**
* 获取一个字符串
* @param
* @return
*
*/
private void getStr(String name,int age){
System.out.println("私有方法,你用不到吧!");
}
//受保护的方法
protected boolean getresult(){
System.out.println("受保护的方法");
return this.test;
}
}