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;
    }
     
  • 相关阅读:
    MyBatis框架(一)
    开始约定编程——Spring AOP
    Spring Boot快速入门
    全注解下的Spring IoC
    Java泛型
    Java异常
    windows监控web程序连接数
    winform导出excel报'object' does not contain a definition for 'get_Range'的问题
    git基本操作
    .net core中使用HttpClient碰到的问题:This instance has already started one or more requests. Properties can only be modified before sending the first request
  • 原文地址:https://www.cnblogs.com/zhangsf/p/3331659.html
Copyright © 2011-2022 走看看