zoukankan      html  css  js  c++  java
  • [零基础学JAVA]Java SE面向对象部分.面向对象基础(02)

    String类

     JAVA:
    public class StringTest{

        public static void main(String args[]){

            //尽量避免使用new,这样会产生内存垃圾

            String name = new String("zsf");
            String welcome= "hello world";
            String hello  = "hello world";
            //对于直接赋值,会将字符串保存在内存池中,不会新开内存
            //new 刚不同。
            //字符串一旦声明,就无法更改,如果出现下面情况,则内存开销相当大

            for (int i = 0;i<100;i++)
                hello += i;

            System.out.println(hello);

            //此hello要不断的断开已有的连接,重新指向新的连接100次,
            //即hello要重新开100次内存,并重连接。

            String str = name ;//通过构造方法完成

            System.out.println(name);
            System.out.println(welcome);
            System.out.println(str);

            if(str.equals(name)){
                System.out.println("true");
            }
            if(str==name){
                System.out.println("true");
            }

            if("zsf".equals(name)){
                System.out.println("true");
            }

        }
    }
    c++:

    #include <iostream>
    #include <string>
    #include <cstdlib>

    using namespace std;



    int main(int argc,char *argv[]){


        string *name = new string("zsf");

        string welcome= "hello world";
        string come= "hello world";

        cout<<*name<<endl;
        //重新分内存,与 java 不同
        cout<<&welcome<<endl;
        cout<<&come<<endl;

        for (int i =0;i<10;i++)
          come+=i;

        //在原来基础上新开内存,若是new出的内存刚不行
        cout<<&come<<endl;
        cout<<come<<endl;

        delete name;

        return 0;
    }
     
  • 相关阅读:
    理解Python闭包,这应该是最好的例子
    2021-01-31
    论unity中UI工具与GUI函数
    2021-01-31
    第八届“图灵杯”NEUQ-ACM程序设计竞赛(全题解&&详细)
    第八届“图灵杯”NEUQ-ACM程序设计竞赛个人赛(同步赛)全题解
    Go-快速排序
    网络地址转换NAT原理及其作用
    解析私有IP地址和公网IP地址
    first blog
  • 原文地址:https://www.cnblogs.com/zhangsf/p/3331659.html
Copyright © 2011-2022 走看看