常用类

Java中 存在 一些类,可以方便开发。对这些类的使用 ,不用强行记忆,只需要 大概知道 哪些常用的类,其类的功能,其中的一些方法。
对任一类,从下面几点把握:
(1)是否为抽象类,构造方法,创造对象
(2)其是否有父类,从父类继承方法,是否有子类
(3)其中的一些常用方法把握

类中的static方法:可以不实例化该类,即不用创建该类对象,直接通过      类名.方法名()方式进行调用;

关键是看懂API.

Object类

基本上所有的类都继承Object类
在java.lang.Object包下
常用方法
equals() ; 比较的是内存地址 需要重写
toString() ; 返回对象的字符串表达形式,需要重写

Object o1 = new Object();
	Object o2 = new Object();
	Object o3 = o1;
	
	System.out.println(o1); //java.lang.Object@15db9742
	System.out.println(o1.toString()); //java.lang.Object@15db9742
	System.out.println(o1.equals(o3));//true

String类

/*java.lang.Object
java.lang.String

	常用方法: 
			charAt(int index)  :返回index索引位置上的字符
			concat(String str) :将字符串拼接到本字符串的末尾位置
			endsWith(String suffix) :判断这个字符串是否以suffix结尾
			equals(Object anObject) :判断字符串是否与anObject相等,已经重写该继承的方法
			equalsIgnoreCase(String annotherString):判断字符串是否与另外一个字符串相等,忽略大小写
			indexOf(String str) :返回字符串中首次出现str的索引位置
			isEmpty()           :返回字符串是否为空
			lastIndexOf(String str):返回字符串 中最后出现(即从右往左的顺序返回索引位置)
			length()             :字符串的长度
			startsWith(String prefix) :字符串是否以某个字符串开头
			toCharArray()      :Converts this String to a new character array
			toLowerCase()   :将字符串转换为小写的字符串
			toString()    
			toUpperCase()   :将字符串转换为大写的字符串
			trim()     去除首尾空字符串
			contains() //某字符串是否包含另外一个字符串
			String s1 = "zhoujian";
			String s2 ="diudiu";
			String s3 = "zhoujian";
			String s4 = "ZhoUjiAN";
			
			System.out.println(s1.charAt(1));  //h
			
			System.out.println(s1.concat(s2)); //zhoujiandiudiu
			
			System.out.println(s1.endsWith("ian"));//true
			
			System.out.println(s1.startsWith("zh")); //true
			
			System.out.println(s1.equals(s3));  //true
			
			System.out.println(s1.equalsIgnoreCase(s4));//true
			
			System.out.println(s1.indexOf("ou"));//2,不包含则返回-1
		
			System.out.println(s1.isEmpty()); //false
			
			System.out.println(s1.length());  //8
			
			char[] c = s1.toCharArray();      //输出 z h o u j i a n
			for(int i =0;i<c.length;i++) {
				System.out.println(c[i]);
			}
			
			System.out.println(s1.contains("oujian")); //true ,是否包含某个子字符串
			
			System.out.println(s1); //zhoujian
			
			
			System.out.println(s1.toString());//与上面一行等
			

StringBuffer类

java.lang.Object
java.lang.StringBuffer

StringBuffer是一个字符串缓冲区,如果需要频繁的对字符串进行拼接时,建议使用StringBuffer

对象的构造方法:

StringBuffer():创建一个大小为16charcater的string buffer

StringBuffer(int capacity) :Constructs a string buffer with no characters in it and the specified intia`l capacity

对象的方法:
append(E e) :添加元素

insert(int offset ,E e) 在offset位置插入元素

capacity() :returns the current capacity

charAt(int index):return the char value in this sequence at the specified index

deleteCharAt(int index) :删除在index索引位置的数据

indexOf(String str) :returns the index within this string of the first occurence of the specified
substring
length() :长度
everse() 翻转
setCharAt(int index,char ch) :The character at the specified index is set to ch
toString();

 
		StringBuffer sb = new StringBuffer();
		
		sb.append("a");
		sb.append("b");
		sb.append("c");
		sb.append("d");
		System.out.println(sb); // abcd
		
		sb.insert(2, "fgh");
		
		System.out.println(sb); // abfghcd
		
		System.out.println(sb.capacity()); //16
		
		System.out.println(sb.length()); //7    注意length 与capacity的区别
		
		
		System.out.println(sb.charAt(0)); //a
		
		System.out.println(sb.deleteCharAt(0));//bfghcd
		
		System.out.println(sb.reverse()); //dchgfb
		
		System.out.println(sb.toString()); //dchgfb

基本类型和包装类

	 		什么是包装类? Java里面8个基本数据类型都有相应的类,这些类叫做包装类
	 		
	 		包装类有什么优点? 可以在对象中定义更多的功能方法操作该数据,方便开发者操作数据,例如       基本数据类型和字符串   之间的转换。
	 		
	 		包装类 都在 java.lang 包里面;下面以 Integer为例来学习一下包装类:
	 		
	 		java.lang.Object
	 			java.lang.Number
	 					java.lang.Integer
	 					
	 		this class provides several methods for converting an int to a String and a String to an int,as well as other 
	 		
	 		constans and methods useful when dealing with an int.
	 					
	 		
	 		构造方法:
	 				Integer(int value)
	 				
	 				Integer(String s)
	 				
	 		对象方法:
	 				
	 				compare(int x , int y) :比较两个数 int
	 				
	 				compareTo(Integer anotherInteger): 
	 				
	 				equals(Object obj):compares the object to the specifed object
	 				
	 				intValue()   :Returns the value of this Integer as an int
	 				
	 				valueOf(int i):Returns a Integer instance representing the specified int value
	 				
	 				parseInt(String s): Parses the String argument as a signed decimal integer
	 				
	 				toString()
	 */

//
// Integer i1 = new Integer(1) ; //创建Integer对象,int—>Interger
// System.out.println(i1); //1
//
// int i2 = i1.intValue(); //Integer -----> int
//
// Integer i3 = new Integer(“123”); //String---->Integer
//
//
// Integer i4 =Integer.valueOf(“123”); //String—>Intege

Date类型

 		在System类下面有个   currentTimeMillis()  :Returns the current time in milliseconds
 		
 		
 		Date
 			java.lang .Object
 				java.util.Data
 				
 		构造方法:
 				Date();
 		
 		
 		对象方法:
 				getTime() :Returns the number of milliseconds since Januaray
 				
 		
 		
 		
 		
 		
 		
 		SimpleDateFormat日期格式化类
 			SimpleDateFormat is a concrete class for formatting and parsing dates in a local-sensitive manner
 			SimpleDateFormat allows you to start by choosing any user-defined patterns for date-time fomatting 
 			
 				java.lang.Object
 					java.textFormat
 						java.text.DateFormat
 							java.text.SimpleDateFormat
 							
 		构造方法:
 		         SimpleDateFormat()  :Constructs a SimpleDateFormat using the default pattern and date format symbols for the default FORMAT local
 		         
 		         
 				SimpleDateFormat(String pattern )
 				
 		
 		对象方法: 
 		 		formate(Date date) :Formatlts the given Date into a date/time string and appends the result to the given StringBuffer.
 		 							
 		 		parse(String text) :Parses text from a string to produce a Date.
 		
 
 
 
 
 
 		Calendar类:  public abstract class Calendar extends
 		             为操作日历提供了一个抽象类
 		              java.lang.Object
 		              		java.util.Calendar
 		              		Calendar provides a class method,getInstance ,for getting a generally useful object of this type.Calendar's
 		              		
 		              		getInstance method returns a Calendar object whose calendar fields have been initialized with the current date and time
 		              		
 		      构造方法:
 		              		Calendar rightNow = Calendar.getInstance();
 		
 		      对象方法:
 		      			 	get(int field)  //Returns the value of the given calendar field
 		      			 
 							setTime(Date date) :Sets this Calendar's time with the given Date.
	Date date = new Date();
	System.out.println(date); //Mon Feb 25 20:27:12 CST 2019
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
	System.out.println(sdf.format(date));  //将时间格式化输出
	
	//将String类型转换为Date类型
	//1、创建格式化对象
	SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
	//2、将字符串转换为Date类型
	String time = "2008-08-08";
	try {
		System.out.println(sdf1.parse(time));
	} catch (ParseException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
			
			
			

	Calendar rightNow = Calendar.getInstance();
	System.out.println(rightNow);
	//查看当前日期是星期几
	int i =rightNow.get(Calendar.DAY_OF_WEEK);
	System.out.println(i);
	
	
	/*
	 * 编写代码获取2008年8月8日是星期几
	 */
	
	String time1 ="2008-08-08";
	SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
	try {
		Date d2 = sdf2.parse(time);
		rightNow.setTime(d2);
		System.out.println(rightNow.get(Calendar.DAY_OF_WEEK));
	} catch (ParseException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}

Math类

	/*
	 	java.lang.Object
	 		java.lang.Math
	 		
	 		The class Math contains methods for performing basic numeric operations such as the elementary expential
	 		
	 		其中的成员变量与方法均为 :静态。
	 			成员变量:  E、PI
	 			
	 			对象方法:
	 					abs()
	 					ceil()
	 					cos()
	 					exp()
	 					floor()
	 					log()
	 					log10()
	 					max()
	 					min()
	 					pow()
	 
	 */

BigInteger

	            java.lang.Number
	            		java.math.BigInteger
	         
	         Immutable arbitrary-precision integers.
	 
	 	BigInteger类可以让超过Integer范围的数据进行运算,通常在对数字计算比较大的行业中应用的多一些。
	 	
	 	构造方法:
	 			BigInteger(String val)          //translates the decimal String representation of a BigInteger into a BigInteger
	 			
	 	对象方法:
	 			abs()
	 			
	 			add(BigInteger val)
	 			
	 			and(BigInteger val)
	 			
	 			divide(BigInteger val) :Returns a Biginteger whose value is (this/val).
	 			
	 			intValue()  :Converts this BigInteger to an int.
	 			
	 			max(BigInteger val):
	 			
	 			subtract(BigInteger val):Returns a BigInteger whose value is (this-value)

**

BigDecimal

**


##

		 		java.lang.Object
		 			java.lang.Number
		 				java.math.BigIInteger
		 				
		 				由于在运算的时候,float类型和double类型很容易丢失精度,在金融、银行等对数值精度要求非常高的领域里面,
		 				就不能使用float和double了,为了能精确的表示、计算浮点数,Java提供了BigDecimal。
		 				注意:如果对计算的数据要求高精度时,必须使用BigDecimal类。
		 				
		 				
		 		构造方法:
		 				BigDecimal(BigInteger val)
		 				
		 				BigDecimal(int val) :Translate an int into a BigDecimal
		 				
		 				BigDecimal(String val): Translate the String representation of a BigDecimal into a Decimal
		 				
		 	  对象方法:
		 	  			abs()
		 	  			
		 	  			add(BigDecimal augend)
		 	  			
		 	  			divide(BigDecimal divisor)
		 	  			
		 	  			equals(Object x)
		 	  			
		 	  			max(BigDecimal val)
		 	  			
		 	  			min(BigDecimal val)
		 	  			
		 	  			multiply(BigDecimal multiplicand)
		 	  			
		 	  			plus()
		 	  			
	//BigDecimal类
		System.out.println(2.0-1.1); //0.8999999999999999;这种方式在开发中不推荐,因为不够精确
		
		//这种方式在开发中不推荐,因为不够精确
		BigDecimal bd1 = new BigDecimal(2.0);
		BigDecimal bd2 = new BigDecimal(1.1);
		System.out.println(bd1.subtract(bd2));//这种方式在开发中不推荐,因为不够精确
		
		//开发时推荐通过传入字符串的方式
		BigDecimal bd3 = new BigDecimal("2.0");
		BigDecimal bs4 = new BigDecimal("1.1");
		System.out.println(bd3.subtract(bs4)); //0.9
		
		//这种方式在开发中也是推荐的
		BigDecimal bd5 = BigDecimal.valueOf(2.0);
		BigDecimal bd6 = BigDecimal.valueOf(1.1);
		System.out.println(bd5.subtract(bd6)); //0.9

NumberFormat类

			java.textFormat
				java.text.NumberFormat
				NumberFormat is the abstract base class for all number formats.This class provides the interface
				for formatting and parsing numbers.
				
			
			
				myString = NumberFormat.getInstcnce().format(); //To format a number for the current Local,use one of the factory class methods:
				
				getCurrencyInstance()  :Returns a currency format for the current default FORMAT local.
				
				parse()   :parse text from the beginning of the given string to produce a number

DecimalFormat类

			在一些金融或者银行的业务里面,会出现这样千分位格式的数字,¥123,456.00,表示人民币。
			DecimalFormat is a concrete subclass of NumberFormat that formats decimal numbers.
			构造方法:
					DecimalFormat()  :Creates a DecimalFormat using the default pattern and symbols for the default FORMAT local.
					
					DecimalFormat(String pattern):Creates a DecimalFormat using the given pattern and symbols for the default FORMAT local.
					
					
		    对象方法:
		    		
		    		format(double number)  //Formats a double to produce a string
		    		
		    		parse(String text)    //Parse text from a string to produce a Number
		    		
		    		
					getCurrency()   //Gets the currency used by this decimal format when formatting currency values
//		//NumberFormat类
//		
//		NumberFormat nf = NumberFormat.getCurrencyInstance();
//		System.out.println(nf.format(123456)); //¥123,456.00
//		
//		try {
//			System.out.println(nf.parseObject("¥123,456.00")); //123456
//		} catch (ParseException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//
//		
//	
//		//DecimalFormat类
//		
//		DecimalFormat df1 = new DecimalFormat();
//		System.out.println(df1.format(1234567)); //1,234,567
//
//		
//		//加入千分位,保留两位小数
//		DecimalFormat df2 = new DecimalFormat("###,###.##");
//		System.out.println(df2.format(1234.567)); //1,234.57
		

Enum类

			在日常开发中可能有一些东西是固定的额,比如一年只有 4个季节,春夏秋冬。我们可以自己定义一个类里面存放这四个季节。在jdk5之后,引入了枚举类(Enum)的概念,可以通过
			
			enum去定义这四个季节。
		//使用枚举存放四季
		
		public enum Season{
			Spring,Summer,Autumn,Winter
		}
		
		//调用枚举
		System.out.println(Season.Sping);
		 

Random类

		java.lang.Object
			java.util.Random
			
	An instance of this class is used to generate a stream of puseudorandom numbers.
	
	构造方法:
			
			Random() :Creates a new random number generator.
			
	对象方法:
			nextInt()  :Returns the next pseudorandom,uniformly distributed int value from this random number generrator's sequence
			
			
			
			nextInt(int bound)  : Returns a pseudorandom ,uniformly distributed int value between 0(inclusive) and the specified value(exclusive)
								  
								  drawn from this random number generator's sequence.

Random r = new Random();
	
	System.out.println(r.nextInt());
	
	System.out.println(r.nextInt(10));
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值