码迷,mamicode.com
首页 > 编程语言 > 详细

学习java第17天

时间:2020-07-22 20:55:48      阅读:80      评论:0      收藏:0      [点我收藏+]

标签:key   app   public   方法   stat   计算   val   属性   elements   

1.“==”与equals的区别

== 是引用是否相等,equals是内容相等

Integer a = new Integer(1);

Integer b = new Integer(1);

if (a = b)...         //错误

if(a.equals(b))...       //正确

public class TestEqualsString {
  public static void main(String[] args) {
    String name1 = new String("LiMing");
    String name2 = new String("LiMing");
    System.out.println( name1==name2 );                            //两个对象的引用,不相等
    System.out.println( name1.equals(name2) );                  // 内容,相等
   
    String name3 = "LiMing";
    String name4 = "LiMing";
    System.out.println( name3==name4 );                               //相同常量的引用,相等
    System.out.println( name3.equals(name4) );                     // 内容,相等
 }
}

2.getClass()

getClass()方法是final方法,不能被重载

3.toString()

用来返回对象的字符串表示,常用来显示  System.out.println(person);,也可以用于字符串的加号

4.包装类的特点

*提供常数,如Integer.MAX_VALUE等

*提供了valueOf(String),toString(),用于从字符串转换或换成字符串

*toString(),equais()等方法进行覆盖

5.包装与拆包

Integer I = 10; 即I = Integer.value(10);

int i I;即i= I.intValue();

6.System类

在命令运行Java程序时可使用 -D 选项添加新的系统属性

import java.util.*;
class SystemProperties
{
 public static void main(String[] args)
 {
  Properties props = System.getProperties();
  Enumeration keys = props.propertyNames();
  while(keys.hasMoreElements() ){
   String key = (String) keys.nextElement();
   System.out.println( key + " = " + props.getProperty(key) );
  }
 }
}

7.字符串

第一类:String类,创建后不再做修改和变动

第二类:StringBuffer,StringBuilder类,创建后允许更改和变动

import java.util.*;
class StringAndStringBuffer
{
 public static void main(String[] args)
 {
  String a = "a";
  String s = "";
  StringBuffer sb = new StringBuffer();
  final int N = 10000;
  long t0 = System.currentTimeMillis();
  for( int i=0; i<N; i++) s+=a;
  long t1 = System.currentTimeMillis();
  for( int i=0; i<N; i++) sb.append(a);
  long t2 = System.currentTimeMillis();
  System.out.println(t1-t0);
  System.out.println(t2-t1);
 }
}
8.Math类
做一些常用的数字计算
public class TestMath{
     public static void main (String args[])
     {  
   System.out.println("Math.ceil(3.1415)=" + Math.ceil(3.1415));
   System.out.println("Math.floor(3.1415)=" + Math.floor(3.1415));
   System.out.println("Math.round(987.654)=" + Math.round(987.654));
   System.out.println("Math.max(-987.654,301)=" + Math.max(-987.654,301));
   System.out.println("Math.min(-987.654,301)=" + Math.min(-987.654,301));
   System.out.println("Math.sqrt(-4.01)=" + new Double(Math.sqrt(-4.01)).isNaN());
   System.out.println("Math.PI=" + Math.PI);
   System.out.println("Math.E=" + Math.E);
     }
}
9.StringBuffer类
构造方法:
StringBuffer()
StringBuffer(int cap)
StringBuffer(String intitial)
10.字符串的分割
java.util.StringTokenizer类提供对字符串进行分割的功能
构造:StringTokenizer(String str,String delim);
 
明天学习内容:
集合,排序与查找
 
 

 

学习java第17天

标签:key   app   public   方法   stat   计算   val   属性   elements   

原文地址:https://www.cnblogs.com/SirNie/p/13362447.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!