>  기사  >  Java  >  Java 개발 요약의 개발 기본 사항

Java 개발 요약의 개발 기본 사항

无忌哥哥
无忌哥哥원래의
2018-07-19 10:54:441147검색

1.기본형

1.1 저장공간

바이트형 바이트 1바이트
성형외과 짧은 2바이트 정수 4바이트 긴 8바이트
부동소수점 뜨다 4바이트 더블 8바이트
문자 유형 숯 2바이트
부울 부울 1바이트

1.2char

1.2.1 char a = 'u0041'은 원래 유니코드 사양에 따라 한자를 나타낼 수 있습니다.

1.2.2 char a = 99

a 직접 비교할 수 있습니다:

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

2 .Operator

2.1 삼항 연산자

부울 표현식? 표현식 1: 표현식 2

연습:

x>0일 때: sgn(x)=1;

x=0일 때: sgn( x)=0 ;

x

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 연산자 우선 순위

&#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;

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(&#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());
	}

}

4.Array

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 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);

위 내용은 Java 개발 요약의 개발 기본 사항의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.