java新手笔记24 Math/String对象

1.Math

package com.yfs.javase;

public class MathDemo {

	public static void main(String[] args) {
		int r = 2;
		System.out.println("圆的面积 : " + (Math.PI * r  * r));
		System.out.println("半径的平方 : " + (Math.pow(r, 2)));
		System.out.println("16的平方根 : " + (Math.sqrt(16)));
		
		System.out.println("随机数 : " + Math.random());
		
		System.out.println("Math.round(3.4) : " + Math.round(3.4));
		System.out.println("Math.round(3.5) : " + Math.round(3.5));
		
		System.out.println("Math.ceil(3.1) : " + Math.ceil(3.1));
		System.out.println("Math.ceil(3.7) : " + Math.ceil(3.7));
		
		System.out.println("Math.floor(3.1) : " + Math.floor(3.1));
		System.out.println("Math.floor(3.7) : " + Math.floor(3.7));

	}

}

 2.String

package com.yfs.javase;

public class StringDemo {

	public static void main(String[] args) {
		String s1 = "  Hello World hello Java I like JAVA you like jAVA   ";
		
		System.out.println("s1: " + s1);
		System.out.println("s1的长度: " + s1.length());
		//去空格
		s1 = s1.trim();
		System.out.println("s1的长度: " + s1.length());
		System.out.println("s1: " + s1);
		System.out.println("s1第一个字符: " + s1.charAt(0));
		System.out.println("s1最后一个字符: " + s1.charAt(47));
		
		//查找
		System.out.println("s1大写 : " + s1.toUpperCase());
		System.out.println("s1小写 : " + s1.toLowerCase());
		System.out.println("s1是否包含'java' : " + s1.contains("java"));
		System.out.println("s1是否包含'java' : " + s1.toLowerCase().contains("java"));
		
		//查找索引
		System.out.println("s1.indexOf('java') : " + s1.toLowerCase().indexOf("java"));
		System.out.println("s1.indexOf('java',19) : " + s1.toLowerCase().indexOf("java",19));
		System.out.println("s1.lastIndexOf('java') : " + s1.toLowerCase().lastIndexOf("java"));
		
		//比较
		String s2 = new String("aBc");
		String s3 = new String("abC");
		System.out.println("s2.equals(s3) : " + s2.equals(s3));
		System.out.println("s2.equalsIgnoreCase(s3) : " + s2.equalsIgnoreCase(s3));
		System.out.println("s3.compareTo(s2) : " + s3.compareTo(s2));
		System.out.println("s2.compareToIgnoreCase(s3) : " + s2.compareToIgnoreCase(s3));
        //包含字符
		String s4 = "C:\javase\Hello.java";
		System.out.println("s4在c盘?" + s4.startsWith("C:"));
		System.out.println("s4是否java文件?" + s4.endsWith(".java"));
		
		//截取
		s4 = s1.substring(18);
		System.out.println("s4 = " + s4);
		System.out.println("s1.substring(6,11) = " + s1.substring(6,11));
		
		s4 = "2014-7-1";
		String[] as = s4.split("-");
		for (int i = 0; i < as.length; i++) {
			System.out.println(as[i]);
		}
		//替换
		System.out.println(s1.replace("l", "*"));
		int a = 15;
		String s5 = String.valueOf(a);//类型转换
	}

}

 3.Integer

package com.yfs.javase;

public class WrapDemo {

	public static void main(String[] args) {
		int a = 15;
		//a.toString();
		//包装
		Integer o1 = new Integer(a);//整型对象
		String s1 = o1.toString();
		System.out.println("s1 = " + s1);
        //访问属性
		System.out.println("最大值: " + Integer.MAX_VALUE);
		
		
		int b = 15;
		Integer o2 = new Integer(b);
		System.out.println("o1 == o2 ? " + (o1 == o2));//比较对象
		System.out.println("a == b ? " + (a == b));//比较数值
		
		Integer o3 = 15;//自动装箱机制new Integer(15);
		
		System.out.println("o3 =  " + o3);
		
		System.out.println("o3 == b ? " + (o3 == b));//自动拆箱
		
		a = o3;//自动拆箱   65   97
		
		char c = 'z';
		Character oc = new Character(c);
		System.out.println("oc.compareTo('Z')  : " + oc.compareTo('Z'));
		
	}

}
原文地址:https://www.cnblogs.com/feilongblog/p/4753453.html