回顾JavaSE(2)-String(1)API构造方法

温故而知新,在Android开发的道路上越走越远的同时,不能忘了JavaSE。

String,最常用的类,没有之一。

先看最好的教材,官方API文档。

  • public final class String
    extends Object
    implements Serializable, Comparable<String>, CharSequence
    The String class represents character strings. All string literals in Java programs, such as"abc", are implemented as instances of this class.

    Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable they can be shared. For example:

         String str = "abc";
     

    is equivalent to:

         char data[] = {'a', 'b', 'c'};
         String str = new String(data);


String类概述
字符串是由多个字符组成的一串数据(字符序列)
字符串可以看成是字符数组

它的构造方法很多,我们重点看这几个:
构造方法:
public String()
public String(byte[] bytes)
public String(byte[] bytes,int offset,int length)
public String(char[] value)
public String(char[] value,int offset,int count)
public String(String original)

先重点看它里面的length方法:

  • public int length()
    Returns the length of this string. The length is equal to the number of Unicode code units in the string.
    Specified by:
    length in interface  CharSequence
    Returns:
    the length of the sequence of characters represented by this object.
是时候看一波源代码:

public class StringTest {

	/**
	 * @param args
	 * 权兴权意-20160919
	 * 字符串:多个字符组成的一串数据,也可以看做是字符数组
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String s1 = new String();
		System.out.println("s1:" + s1);
		System.out.println("s1.length():" + s1.length());
		
		System.out.println("--------------");
		
		byte[] b = {97,98,99,100};
		String s2 = new String(b);
		System.out.println("s2:" + s2);
		System.out.println("s2.length():" + s2.length());
		
		System.out.println("--------------");
		
		String s3 = new String(b,1,2);
		System.out.println("s3:" + s3);
		System.out.println("s3.length():" + s3.length());
		
		System.out.println("--------------");
		
		char[] c = {'a','b','c','d','权'};
		String s4 = new String(c);
		System.out.println("s4:" + s4);
		System.out.println("s4.length():" + s4.length());
		
		System.out.println("--------------");
		
		String s5 = new String(c,1,2);
		System.out.println("s5:" + s5);
		System.out.println("s5.length():" + s5.length());
		
		System.out.println("--------------");
		
		String s6 = new String("abcd");
		System.out.println("s6:" + s6);
		System.out.println("s6.length():" + s6.length());
		
		System.out.println("--------------");
		
		String s7 = "abcd";
		System.out.println("s7:" + s7);
		System.out.println("s7.length():" + s7.length());
	}

}











  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
面向对象编程是一种编程范式,它将程序的构建和设计思路以面向对象的方式进行组织和实现。在Java中,面向对象编程是基于Java SE(Standard Edition)的一种编程方式。第07讲主要介绍了面向对象编程中的一些基本概念和关键术语。 在面向对象编程中,我们将程序中的数据和对数据的操作(方法)封装在一起,形成一个对象。对象由两部分构成:属性和方法。属性是用来描述对象的特征,而方法则是对象可以执行的操作。对象之间通过消息(方法调用)进行通信和交互。面向对象的核心思想是通过封装、继承和多态实现程序的复用和扩展。 封装是面向对象编程中的一个重要概念,它指的是将类的属性和方法进行封装,使得外部无法直接访问和修改对象的内部状态,只能通过公共的方法来操作属性和执行方法。封装提供了一种将数据和行为组合在一起的方式,可以保护数据的完整性和安全性。 继承是面向对象编程中的另一个重要概念,它指的是通过定义一个新的类来继承现有类的属性和方法。通过继承,子类可以继承父类的属性和方法,并可以在此基础上进行扩展和修改。继承提供了一种代码复用的机制,可以减少重复编码的工作量。 多态是面向对象编程的又一个重要概念,它指的是同一类型的对象在不同的情况下可以有不同的表现形式。多态通过方法的重写和方法的重载实现。方法的重写指的是在子类中重新定义和实现父类的方法方法的重载指的是在同一个类中可以定义多个同名但参数列表不同的方法。 总结来说,面向对象编程是一种将程序组织和设计思路以对象为中心的编程方式。在JavaSE中,我们可以通过封装、继承和多态来实现面向对象编程的目标。封装可以提高程序的可维护性和可复用性,继承可以减少重复编码的工作量,多态可以灵活地操作对象。掌握这些基本概念和关键术语,可以帮助我们更好地理解和应用面向对象编程的思想。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值