package cn.hncu.day2;

public class StringDemo {

public static void main(String[] args) {
String str1 = "abc";//直接赋值,放在栈中,数据共享
String str2 = "abc";
System.out.println("str1==str2: "+(str1==str2));//true

String str3 = new String("abc");//new的东西是在堆中开的,每次都新开空间,因此内存地址是不一样的
String str4 = new String("abc");
System.out.println("str3==str4: "+(str3==str4));//false,"=="是判断栈中的值是否相同

System.out.println("str1==str4: "+(str1==str4));

System.out.println("str1.equals(str4): "+str1.equals(str4));

//综上,以后我们做项目时,判断字符串变量是否相等,一定要用equals()方法

}

}

------------------------------------------------------------------------------------------------------------

package cn.hncu.day2;

public class BoxDemo {

public static void main(String[] args) {
//demo1();
demo2();
}
private static void demo2(){
Integer i1 = 100;
Integer i2 = 100;
System.out.println("i1==i2: "+(i1==i2));

Integer i3 = 200;
Integer i4 = 200;
System.out.println("i3==i4: "+(i3==i4));

int a=100;
int b=100;
System.out.println(a==b);

}

private static void demo1() {
Integer a1 = new Integer(5);
System.out.println(a1);

//自动装箱
Integer a2 = 15;
System.out.println(a2);

//自动拆箱
int x = new Integer(10);
System.out.println(x);

//混合使用
int sum = a2+x;
System.out.println("sum="+sum);
}

}

--------------------------------------------------------------------------------

package cn.hncu.day2;

public class Ball {
private double height;
private int downTimes;
private int jumpTimes;
private double sum;
public Ball(double height) {
this.height = height;
}
public void jump(){
height = height/2;
jumpTimes++;
sum = sum + height;
}
public void fall(){
downTimes++;
sum = sum + height;
}

public static void main(String[] args) {
//第10次落地时,共经过多少米?
Ball ball = new Ball(100);
while(true){
ball.fall();
if(ball.downTimes==10){
System.out.println(ball.sum);
break;
}
ball.jump();
}

//第10次反弹多高?
Ball ball2 = new Ball(100);
while(true){
ball2.fall();
ball2.jump();
if(ball2.jumpTimes==10){
System.out.println(ball2.height);
break;
}
}

System.out.println("-------------");
test2();

}

//用面向过程的方式求解
private static void test2() {
double sum =0;
double begin=100;
double fanTan=0;
for(int i=0; i<10; i++ ){
if(i==9){//第10次落地
sum = sum + begin;
fanTan = begin/2;
}else{
sum = sum + begin + begin/2;
}
begin = begin/2;
}
System.out.println(sum+","+fanTan);
}

}

java String的比较,BOX装箱拆箱,以及面向对象的小代码的更多相关文章

  1. java中的包装类与装箱拆箱定义

    JAVA 中int类型转String类型的通常方法,有三种:  1.String.valueOf(int i)  2.Integer.toString(int i)  3.i+"" ...

  2. java中Integer与int装箱拆箱一点收获

    示例代码: class BoxIntInteger { public static void main(String[] args) { Integer a = new Integer(10111); ...

  3. Java 性能要点:自动装箱/ 拆箱 (Autoboxing / Unboxing)

    [编者按]本文作者为 Ali Kemal TASCI,最早于2016年4月9日发布于DZONE社区.文章主要介绍通过改进 Java 1.5 就已存在的骨灰级特性大幅度提高应用性能. 本文系 OneAP ...

  4. JDK5.0新特性(静态导入、自动装箱/拆箱、增强for循环、可变参数、枚举、泛形)

    JDK5中新增了很多新的java特性,利用这些新语法可以帮助开发人员编写出更加高效.清晰,安全的代码. 这些新特性主要有:1.静态导入2.自动装箱/拆箱3.增强for循环4.可变参数5.枚举6.泛型7 ...

  5. Java中的装箱拆箱

    一)  装箱与拆箱 Java中有概念是一切皆对象,因为所有的类都默认继承自Object.但是,对于数据类型是个例外,如short,int,long,float,double, byte,char,bo ...

  6. Java 装箱 拆箱

    Java 自动装箱与拆箱   ??什么是自动装箱拆箱 基本数据类型的自动装箱(autoboxing).拆箱(unboxing)是自J2SE 5.0开始提供的功能. 一般我们要创建一个类的对象的时候,我 ...

  7. Java 的自动装箱拆箱

    Java 是面向对象的语言,其基本数据类型也就有了相对应的类,称为包装类.以下是基本数据类型对应的包装类: 基本数据类型 包装类 byte(1字节) Byte short(2字节) Short int ...

  8. Java之集合初探(二)Iterator(迭代器),collections,打包/解包(装箱拆箱),泛型(Generic),comparable接口

    Iterator(迭代器) 所有实现了Collection接口的容器都有一个iterator方法, 用来返回一个实现了Iterator接口的对象 Iterator对象称作迭代器, 用来方便的实现对容器 ...

  9. Java八种基本数据类型的大小,以及封装类,自动装箱/拆箱的用法?

    参考:http://blog.csdn.net/mazhimazh/article/details/16799925 1. Java八种基本数据类型的大小,以及封装类,自动装箱/拆箱的用法? 原始类型 ...

随机推荐

  1. JAVA实现前几秒几分钟几天前几年源码

    package com.date; import java.text.ParseException; import java.text.SimpleDateFormat; import java.ut ...

  2. Sectong日志分析

    http://tech.uc.cn/?p=2866#comments http://blog.sectong.com/blog/hw_bigdata.html

  3. log.isDebugEnabled()的使用

    转自: http://huangxx.iteye.com/blog/190693 在使用log4j,common-log这样的log框架时,发现很多代码中这样写 if   (log.isDebugEn ...

  4. [Mac]使用xcode命令行编译protobuf

    因为不想每次测试与画面显示无关的小功能时把cocos2dx包含进去,所以改用xcode的command line tool直接建立c++项目. 方法: 1.点击file-new-project,选择o ...

  5. WordPress WooCommerce ‘hide-wc-extensions-message’参数跨站脚本漏洞

    漏洞名称: WordPress WooCommerce ‘hide-wc-extensions-message’参数跨站脚本漏洞 CNNVD编号: CNNVD-201310-501 发布时间: 201 ...

  6. Apache Commons IO 2.3 几点用法

    //直接将IO流转成字符串 InputStream in = new URL( "http://jakarta.apache.org" ).openStream(); try { ...

  7. win7重装系统的配置步骤

    0. 关闭休眠功能,在开始菜单的运行里输入powercfg -h off 指令,关闭休眠,此文件实际大小和物理内存是一样的,大约可以为C盘释放1-3G的空间. 1. 在Win7桌面上显示“我的电脑” ...

  8. Total Commander快捷键

    (1)tab键---切换左.右两个大的窗口:

  9. Kia's Calculation(HDU 4267)

    Problem Description Doctor Ghee is teaching Kia how to calculate the sum of two integers. But Kia is ...

  10. Unity给力插件之Final IK

    Final IK细节: 1.Aim IK:设定一个目标,关节末端始终朝向该目标,一般用来做头部的朝向. 步骤: a.在模型头节点处添加Aim空物体并reset b.给模型添加Aim IK组件,并填上A ...