1.基本类型1.1储存空间字节型 byte 1字节
整形 short 2字节 int 4字节 long 8字节
浮点型 float 4字节 double 8字节
字符型 char 2字节
布尔型 boolean 1字节
1.2char
1.2.1 char a = '\u0041' 可表示一个中文汉字,基于原始的 unicode 规范
1.2.2 char a = 99
a可以直接比较:
char a = 99;if (a < 'z' && a > 'a') { system.out.println(a);}
2.运算符2.1三目运算符boolean表达式 ? 表达式1 : 表达式2
练习:
当x>0: sgn(x)=1;
当x=0: sgn(x)=0;
当x<0: sgn(x)=-1;
输入x,输出sgn(x)的值。
public static void function04(){ system.out.println("请输入x的值:"); scanner scan = new scanner(system.in); int x = scan.nextint(); system.out.println("sgn(x)=" + (0==x?0:(x>0?1:-1)));}
2.2运算符优先级'{}' > '++' > '(强制类型转换)' > '/' > '+' > '<<' > '>=' > '==' > '&' > '^' > '|' > '&&' > '||' > '?:' > '='
2.3 "equals()"与 "=="equals
注意:equals方法不能作用于基本数据类型的变量。
如果没有对equals方法进行重写,则比较的是引用类型的变量所指向的对象的地址;
诸如string、date等类对equals方法进行了重写的话,比较的是所指向的对象的内容。
==
如果作用于基本数据类型的变量,则直接比较其存储的 “值”是否相等;
如果作用于引用类型的变量,则比较的是所指向的对象的地址。
3.string/** * 1:输出字符串"helloworld"的字符串长度 * 2:输出"helloworld"中"o"的位置 * 3:输出"helloworld"中从下标5出开始第一次出现"o"的位置 * 4:截取"helloworld"中的"hello"并输出 * 5:截取"helloworld"中的"world"并输出 * 6:将字符串" hello "中两边的空白去除后输出 * 7:输出"helloworld"中第6个字符"w" * 8:输出"helloworld"是否是以"h"开头和"ld"结尾的。 * 9:将"helloworld"分别转换为全大写和全小写并输出。 */public class test01 { public static void main(string[] args) { string str = "helloworld"; test1(str); } public static void test1(string str){ system.out.println(str.length()); } public static void test2(string str){ system.out.println(str.indexof('o')); } public static void test3(string str){ system.out.println(str.indexof('o', 5)); } public static void test4(string str){ system.out.println(str.substring(0,5));//substring()内取右不取左 } public static void test5(string str){ system.out.println(str.substring(5)); } public static void test6(string str){ system.out.println(str.trim()); } public static void test7(string str){ system.out.println(str.charat(5)); } public static void test8(string str){ system.out.println(str.startswith("h")+"\n"+str.endswith("ld")); } public static void test9(string str){ system.out.println(str.tolowercase()+"\n"+str.touppercase()); }}
4.数组4.1创建写法int[] arr = new int[10]; //初始化int[] arr = {1,2,3,4,5}; //初始化并赋值int[] arr1 = new int[] {1,2,3,4,5};
4.2数组操作代码//将array转化成set集合set<string> set = new hashset<string>(arrays.aslist(stringarray));system.out.println(set);//[d, e, b, c, a]//数组翻转int[] intarray = { 1, 2, 3, 4, 5 };arrayutils.reverse(intarray);system.out.println(arrays.tostring(intarray));//[5, 4, 3, 2, 1]//从数组中移除一个元素int[] intarray = { 1, 2, 3, 4, 5 };int[] removed = arrayutils.removeelement(intarray, 3);//create a new arraysystem.out.println(arrays.tostring(removed));//将一个int值转化成byte数组byte[] bytes = bytebuffer.allocate(4).putint(8).array();for (byte t : bytes) { system.out.format("0x%x ", t);}//检查数组中是否包含某一个值string[] stringarray = { "a", "b", "c", "d", "e" };boolean b = arrays.aslist(stringarray).contains("a");system.out.println(b);// true//连接两个数组int[] intarray = { 1, 2, 3, 4, 5 };int[] intarray2 = { 6, 7, 8, 9, 10 };// apache commons lang libraryint[] combinedintarray = arrayutils.addall(intarray, intarray2);//将数组中的元素以字符串的形式输出string j = stringutils.join(new string[] { "a", "b", "c" }, ", ");system.out.println(j);
以上就是java开发总结之开发基础的详细内容。
