Home > Java > javaTutorial > Development basics of Java development summary

Development basics of Java development summary

无忌哥哥
Release: 2018-07-19 10:54:44
Original
1195 people have browsed it

1.Basic type

1.1 Storage space

##Byte type byte 1 byte Plastic surgery short 2 bytes int 4 bytes long 8 bytes Floating point type float 4 bytes double 8 bytes Character type char 2 bytes Boolean boolean 1 byte
1.2char

##1.2.1 char a = '\u0041' can represent a Chinese character, based on the original Unicode specification

1.2.2 char a = 99

a can be directly compared:

char a = 99;
if (a < &#39;z&#39; && a > &#39;a&#39;) {
    System.out.println(a);
}
Copy after login

2. Operator

2.1Ternary operator

Boolean Expression? Expression 1: Expression 2

Exercise:

When x>0: sgn(x)=1;

When x=0: sgn(x )=0;

When x<0: sgn(x)=-1;

Input x and output the value of 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)));
}
Copy after login

2.2 Operator precedence

&#39;{}&#39;  >  &#39;++&#39;  >  &#39;(强制类型转换)&#39;  >  &#39;/&#39; >  &#39;+&#39;  >  &#39;<<&#39;  >  &#39;>=&#39;  >  &#39;==&#39;  >  &#39;&&#39;  >  &#39;^&#39;  >  &#39;|&#39;  >  &#39;&&&#39;  >  &#39;||&#39;  >  &#39;?:&#39;  >  &#39;=&#39;
Copy after login

2.3 "equals()" and "=="

equals

Note: the equals method cannot act on Variables of basic data types.

If the equals method is not rewritten, the address of the object pointed to by the reference type variable is compared;

If classes such as String and Date have rewritten the equals method , what is compared is the content of the pointed object.

==

If it acts on a variable of a basic data type, directly compare whether the "value" stored in it is equal;

If it acts on a variable of a reference type, then What is compared is the address of the pointed object.

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(&#39;o&#39;));
	}
	public static void test3(String str){
		System.out.println(str.indexOf(&#39;o&#39;, 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());
	}

}
Copy after login

4.Array

4.1 Creation and writing method

int[] arr = new int[10]; //初始化
int[] arr = {1,2,3,4,5}; //初始化并赋值
int[] arr1 = new int[] {1,2,3,4,5};
Copy after login

4.2 Array operation code

//将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 array
System.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 library
int[] combinedIntArray = ArrayUtils.addAll(intArray, intArray2);

//将数组中的元素以字符串的形式输出
String j = StringUtils.join(new String[] { "a", "b", "c" }, ", ");
System.out.println(j);
Copy after login

The above is the detailed content of Development basics of Java development summary. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template