Java by API/java.lang/String
Содержание
- 1 Character: toLowerCase(char ch)
- 2 new String()
- 3 new String(byte[] ascii, int hibyte)
- 4 new String(byte[] bytes)
- 5 new String(byte[] bytes, int offset, int length)
- 6 new String(char[] value)
- 7 new String(char[] value, int offset, int count)
- 8 new String(String original)
- 9 String: boolean endsWith(String suffix)
- 10 String: boolean regionMatches(int toffset, String other, int ooffset, int len)
- 11 String: boolean startsWith(String prefix)
- 12 String: byte[] getBytes()
- 13 String.CASE_INSENSITIVE_ORDER
- 14 String: charAt(int index)
- 15 String: char[] toCharArray()
- 16 String: compareTo(String stringValue)
- 17 String: copyValueOf(char[] data)
- 18 String: copyValueOf(char[] data, int offset, int count)
- 19 String: equals
- 20 String: equalsIgnoreCase
- 21 String: getBytes(String charsetName)
- 22 String: getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
- 23 String: indexOf(int intValue)
- 24 String: indexOf(int intValue, int fromIndex)
- 25 String: indexOf(String stringValue)
- 26 String: indexOf(String stringValue, int fromIndex)
- 27 String: int hashCode()
- 28 String: int length()
- 29 String: lastIndexOf(int intValue)
- 30 String: lastIndexOf(int intValue, int fromIndex)
- 31 String: lastIndexOf(String stringValue)
- 32 String: lastIndexOf(String stringValue, int fromIndex)
- 33 String: matches(String regex)
- 34 String: replace(char oldChar, char newChar)
- 35 String: split(String regex)
- 36 String: String[] split(String regex, int limit)
- 37 String: substring(int beginIndex, int endIndex)
- 38 String: substring(int pos)
- 39 String: toLowerCase()
- 40 String: toUpperCase()
- 41 String: trim()
- 42 String: valueOf(boolean b)
- 43 String: valueOf(char c)
- 44 String: valueOf(char[] data)
- 45 String: valueOf(char[] data, int offset, int count)
- 46 String: valueOf(double d)
- 47 String: valueOf(float f)
- 48 String: valueOf(int i)
- 49 String: valueOf(long l)
- 50 String: valueOf(Object obj)
- 51 == vs equals
Character: toLowerCase(char ch)
public class Main {
public static void main(String[] arg) {
String phrase = "The quick brown fox jumped over the lazy dog.";
int vowels = 0;
for(char ch : phrase.toCharArray()) {
ch = Character.toLowerCase(ch);
if(ch == "a" || ch == "e" || ch == "i" || ch == "o" || ch == "u") {
++vowels;
}
}
System.out.println("The phrase contains " + vowels + " vowels.");
}
}
new String()
public class Main {
public static void main(String args[]) {
char charArray[] = { "b", "i", "r", "t", "h", " ", "d", "a", "y" };
String s = new String("hello");
// use String constructors
String s1 = new String();
String s2 = new String(s);
String s3 = new String(charArray);
String s4 = new String(charArray, 6, 3);
System.out.printf("s1 = %s\ns2 = %s\ns3 = %s\ns4 = %s\n", s1, s2, s3, s4);
}
}
new String(byte[] ascii, int hibyte)
/*
* Output:
a = ABCDEFGHIJ
*/
public class MainClass {
static byte a[] = { 65, 66, 67, 68, 69, 70, 71, 72, 73, 74 };
static byte b[] = { 77, 77, 77, 77, 77, 77, 77, 77, 77, 77 };
public static void main(String args[]) {
System.out.println("a = " + new String(a, 0));
}
}
new String(byte[] bytes)
public class Main {
public static void main(String args[]) {
byte ascii[] = { 65, 66, 67, 68, 69, 70 };
String s1 = new String(ascii);
System.out.println(s1);
String s2 = new String(ascii, 2, 3);
System.out.println(s2);
}
}
new String(byte[] bytes, int offset, int length)
public class Main {
public static void main(String args[]) {
byte ascii[] = { 65, 66, 67, 68, 69, 70 };
String s1 = new String(ascii);
System.out.println(s1);
String s2 = new String(ascii, 2, 3);
System.out.println(s2);
}
}
new String(char[] value)
public class Main {
public static void main(String args[]) {
char charArray[] = { "b", "i", "r", "t", "h", " ", "d", "a", "y" };
String s = new String("hello");
// use String constructors
String s1 = new String();
String s2 = new String(s);
String s3 = new String(charArray);
String s4 = new String(charArray, 6, 3);
System.out.printf("s1 = %s\ns2 = %s\ns3 = %s\ns4 = %s\n", s1, s2, s3, s4);
}
}
new String(char[] value, int offset, int count)
public class Main {
public static void main(String args[]) {
char charArray[] = { "b", "i", "r", "t", "h", " ", "d", "a", "y" };
String s = new String("hello");
// use String constructors
String s1 = new String();
String s2 = new String(s);
String s3 = new String(charArray);
String s4 = new String(charArray, 6, 3);
System.out.printf("s1 = %s\ns2 = %s\ns3 = %s\ns4 = %s\n", s1, s2, s3, s4);
}
}
new String(String original)
public class Main {
public static void main(String args[]) {
char charArray[] = { "b", "i", "r", "t", "h", " ", "d", "a", "y" };
String s = new String("hello");
// use String constructors
String s1 = new String();
String s2 = new String(s);
String s3 = new String(charArray);
String s4 = new String(charArray, 6, 3);
System.out.printf("s1 = %s\ns2 = %s\ns3 = %s\ns4 = %s\n", s1, s2, s3, s4);
}
}
String: boolean endsWith(String suffix)
public class Main {
public static void main(String[] arg) {
String string1 = "abcde";
if(string1.endsWith("de")) {
System.out.println("ends with de");
}
}
}
String: boolean regionMatches(int toffset, String other, int ooffset, int len)
public class Main {
public static void main(String[] args) {
String searchMe = "Green Eggs and Ham";
String findMe = "Eggs";
int searchMeLength = searchMe.length();
int findMeLength = findMe.length();
boolean foundIt = false;
for (int i = 0; i <= (searchMeLength - findMeLength); i++) {
if (searchMe.regionMatches(i, findMe, 0, findMeLength)) {
foundIt = true;
System.out.println(searchMe.substring(i, i + findMeLength));
break;
}
}
if (!foundIt)
System.out.println("No match found.");
}
}
String: boolean startsWith(String prefix)
public class Main {
public static void main(String[] arg) {
String string1 = "abcde";
if(string1.startsWith("ab")) {
System.out.println("starts with ab");
}
}
}
String: byte[] getBytes()
public class Main {
public static void main(String[] arg) {
String text = "To be or not to be";
byte[] textArray = text.getBytes();
for(byte b: textArray){
System.out.println(b);
}
}
}
String.CASE_INSENSITIVE_ORDER
import java.util.ArrayList;
import java.util.Collections;
public class MainClass{
public static void main(String[] av) {
String[] stringArray = new String[]{"c","e","a","k"};
ArrayList<String> list = new ArrayList<String>();
for(String s: stringArray){
list.add(s);
}
Collections.sort(list, String.CASE_INSENSITIVE_ORDER);
for (String s: list) {
System.out.println(s);
}
}
}
String: charAt(int index)
public class Main {
public static void main(String[] args) {
String palindrome = "Dot saw I was Tod";
int len = palindrome.length();
char[] tempCharArray = new char[len];
char[] charArray = new char[len];
// put original string in an array of chars
for (int i = 0; i < len; i++) {
tempCharArray[i] = palindrome.charAt(i);
}
// reverse array of chars
for (int j = 0; j < len; j++) {
charArray[j] = tempCharArray[len - 1 - j];
}
String reversePalindrome = new String(charArray);
System.out.println(reversePalindrome);
}
}
String: char[] toCharArray()
public class Main {
public static void main(String[] arg) {
String text = "To be or not to be";
char[] textArray = text.toCharArray();
for(char ch: textArray){
System.out.println(ch);
}
}
}
String: compareTo(String stringValue)
/*
* Output:
*
N
i
t
time
*
*/
public class MainClass {
public static void main(String args[]) {
String arr[] = { "N", "i", "t", "time" };
for (int j = 0; j < arr.length; j++) {
for (int i = j + 1; i < arr.length; i++) {
if (arr[i].rupareTo(arr[j]) < 0) {
String t = arr[j];
arr[j] = arr[i];
arr[i] = t;
}
}
System.out.println(arr[j]);
}
}
}
String: copyValueOf(char[] data)
public class Main {
public static void main(String[] arg) {
char[] textArray = { "T", "o", " ", "b", "e", " ", "o", "r", " ", "n", "o", "t", " ", "t", "o",
" ", "b", "e" };
String text = String.copyValueOf(textArray);
System.out.println(text);
}
}
String: copyValueOf(char[] data, int offset, int count)
public class Main {
public static void main(String[] arg) {
char[] textArray = { "T", "o", " ", "b", "e", " ", "o", "r", " ", "n", "o", "t", " ", "t", "o",
" ", "b", "e" };
String text = String.copyValueOf(textArray, 9, 3);
System.out.println(text);
}
}
String: equals
/*
* Output:
Hello equals Hello -> true
Hello equals Goodbye -> false
Hello equals HELLO -> false
* */
public class MainClass {
public static void main(String args[]) {
String s1 = "Hello";
String s2 = "Hello";
String s3 = "Goodbye";
String s4 = "HELLO";
System.out.println(s1 + " equals " + s2 + " -> " + s1.equals(s2));
System.out.println(s1 + " equals " + s3 + " -> " + s1.equals(s3));
System.out.println(s1 + " equals " + s4 + " -> " + s1.equals(s4));
}
}
String: equalsIgnoreCase
/*
* Output:
Hello equalsIgnoreCase HELLO -> true
* */
public class MainClass {
public static void main(String args[]) {
String s1 = "Hello";
String s4 = "HELLO";
System.out.println(s1 + " equalsIgnoreCase " + s4 + " -> " +
s1.equalsIgnoreCase(s4));
}
}
String: getBytes(String charsetName)
public class Main {
public static void main(String args[]) throws Exception {
String s = "0123456789";
byte ptext[] = s.getBytes("UTF8");
for (int i = 0; i < ptext.length; i++) {
System.out.print(ptext[i] + ",");
}
}
}
String: getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
/*
* Output:
his
* */
public class MainClass {
public static void main(String args[]) {
String s = "This is an demo.";
int start = 1;
int end = 5;
char buf[] = new char[end - start];
s.getChars(start, end, buf, 0);
System.out.println(buf);
}
}
String: indexOf(int intValue)
/*
* Output:
public static void main(String args[]) {
indexOf(t) = 8
* */
public class MainClass {
public static void main(String args[]) {
String s = "public static void main(String args[]) {";
System.out.println(s);
System.out.println("indexOf(t) = " + s.indexOf("t"));
}
}
String: indexOf(int intValue, int fromIndex)
/*
* Output:
public static void main(String args[]) {
indexOf(t, 10) = 10
* */
public class MainClass {
public static void main(String args[]) {
String s = "public static void main(String args[]) {";
System.out.println(s);
System.out.println("indexOf(t, 10) = " +
s.indexOf("t", 10));
}
}
String: indexOf(String stringValue)
/*
* Output:
public static void main(String args[]) {
indexOf(the) = 0
* */
public class MainClass {
public static void main(String args[]) {
String s = "public static void main(String args[]) {";
System.out.println(s);
System.out.println("indexOf(the) = " +
s.indexOf("pub"));
}
}
String: indexOf(String stringValue, int fromIndex)
/*
* Output:
public static void main(String args[]) {
indexOf(the, 10) = -1
* */
public class MainClass {
public static void main(String args[]) {
String s = "public static void main(String args[]) {";
System.out.println(s);
System.out.println("indexOf(the, 10) = " +
s.indexOf("the", 10));
}
}
String: int hashCode()
public class Main {
public static void main(String[] args) {
System.out.println("Hello".hashCode());
System.out.println("Hello".hashCode());
}
}
String: int length()
public class Main {
public static void main(String[] args) {
String palindrome = "Dot saw I was Tod";
int len = palindrome.length();
char[] tempCharArray = new char[len];
char[] charArray = new char[len];
// put original string in an array of chars
for (int i = 0; i < len; i++) {
tempCharArray[i] = palindrome.charAt(i);
}
// reverse array of chars
for (int j = 0; j < len; j++) {
charArray[j] = tempCharArray[len - 1 - j];
}
String reversePalindrome = new String(charArray);
System.out.println(reversePalindrome);
}
}
String: lastIndexOf(int intValue)
/*
* Output:
public static void main(String args[]) {
lastIndexOf(t) = 25
* */
public class MainClass {
public static void main(String args[]) {
String s = "public static void main(String args[]) {";
System.out.println(s);
System.out.println("lastIndexOf(t) = " +
s.lastIndexOf("t"));
}
}
String: lastIndexOf(int intValue, int fromIndex)
/*
* Output:
public static void main(String args[]) {
lastIndexOf(t, 50) = 25
* */
public class MainClass {
public static void main(String args[]) {
String s = "public static void main(String args[]) {";
System.out.println(s);
System.out.println("lastIndexOf(t, 50) = " +
s.lastIndexOf("t", 50));
}
}
String: lastIndexOf(String stringValue)
/*
* Output:
public static void main(String args[]) {
lastIndexOf(the) = 0
* */
public class MainClass {
public static void main(String args[]) {
String s = "public static void main(String args[]) {";
System.out.println(s);
System.out.println("lastIndexOf(the) = " +
s.lastIndexOf("pub"));
}
}
String: lastIndexOf(String stringValue, int fromIndex)
/*
* Output:
public static void main(String args[]) {
lastIndexOf(the, 50) = -1
* */
public class MainClass {
public static void main(String args[]) {
String s = "public static void main(String args[]) {";
System.out.println(s);
System.out.println("lastIndexOf(the, 50) = " +
s.lastIndexOf("the", 50));
}
}
String: matches(String regex)
public class Main {
public static void main(String args[]) {
String name = "First Name";
String nameToken = "\\p{Upper}(\\p{Lower}+\\s?)";
String namePattern = "(" + nameToken + "){2,3}";
System.out.println(name.matches(namePattern));
}
}
String: replace(char oldChar, char newChar)
public class Main {
public static void main(String[] arg) {
String text = "To be or not to be, that is the question.";
String newText = text.replace(" ", "/");
System.out.println(newText);
}
}
String: split(String regex)
/*
Number of tokens: 5
A
B
C
D
E
*/
public class MainClass {
public static void main(String[] unused) {
String text = "A B, C, D. E"; // String to segment
String delimiters = "\\s+|,\\s*|\\.\\s*";
// Analyze the string
String[] tokens = text.split(delimiters);
// Output the tokens
System.out.println("Number of tokens: " + tokens.length);
for(String token : tokens) {
System.out.println(token);
} }
}
String: String[] split(String regex, int limit)
public class Main {
public static void main(String[] arg) {
String text = "To be or not to be, that is the question.";
String delimiters = "[, .]";
int[] limits = { 0, -1 };
for (int limit : limits) {
System.out.println("\nAnalysis with limit = " + limit);
String[] tokens = text.split(delimiters, limit);
System.out.println("Number of tokens: " + tokens.length);
for (String token : tokens) {
System.out.println(token);
}
}
}
}
String: substring(int beginIndex, int endIndex)
/*
* Output:
This is a test. This is, too.
Thwas is a test. This is, too.
Thwas was a test. This is, too.
Thwas was a test. Thwas is, too.
Thwas was a test. Thwas was, too.
*/
public class MainClass {
public static void main(String args[]) {
String org = "This is a test. This is, too.";
String search = "is";
String sub = "was";
String result = "";
int i;
do {
System.out.println(org);
i = org.indexOf(search);
if (i != -1) {
result = org.substring(0, i);
result = result + sub;
result = result + org.substring(i + search.length());
org = result;
}
} while (i != -1);
}
}
String: substring(int pos)
/*
* Output:
$45.67
*/
public class MainClass {
public static void main(String args[]) {
String s1 = "The total cost is $45.67";
int i1 = s1.indexOf("$");
String s2 = s1.substring(i1);
System.out.println(s2);
}
}
String: toLowerCase()
/*
* Output:
this is a test. this is, too.
*/
public class MainClass {
public static void main(String args[]) {
String org = "This is a test. This is, too. ";
System.out.println(org.toLowerCase());
}
}
String: toUpperCase()
/*
* Output:
THIS IS A TEST. THIS IS, TOO.
*/
public class MainClass {
public static void main(String args[]) {
String org = "This is a test. This is, too. ";
System.out.println(org.toUpperCase());
}
}
String: trim()
/*
* Output:
>This is a test. This is, too. <
>This is a test. This is, too.<
*/
public class MainClass {
public static void main(String args[]) {
String org = "This is a test. This is, too. ";
System.out.println(">"+org+"<");
System.out.println(">"+org.trim()+"<");
}
}
String: valueOf(boolean b)
public class Main {
public static void main(String args[]) {
char charArray[] = { "a", "b", "c", "d", "e", "f" };
boolean booleanValue = true;
char characterValue = "Z";
int integerValue = 7;
long longValue = 10000000000L; // L suffix indicates long
float floatValue = 2.5f; // f indicates that 2.5 is a float
double doubleValue = 3.3; // no suffix, double is default
Object objectRef = "hello"; // assign string to an Object reference
System.out.printf("char array = %s\n", String.valueOf(charArray));
System.out.printf("part of char array = %s\n", String.valueOf(charArray, 3, 3));
System.out.printf("boolean = %s\n", String.valueOf(booleanValue));
System.out.printf("char = %s\n", String.valueOf(characterValue));
System.out.printf("int = %s\n", String.valueOf(integerValue));
System.out.printf("long = %s\n", String.valueOf(longValue));
System.out.printf("float = %s\n", String.valueOf(floatValue));
System.out.printf("double = %s\n", String.valueOf(doubleValue));
System.out.printf("Object = %s\n", String.valueOf(objectRef));
}
}
String: valueOf(char c)
public class Main {
public static void main(String args[]) {
char charArray[] = { "a", "b", "c", "d", "e", "f" };
boolean booleanValue = true;
char characterValue = "Z";
int integerValue = 7;
long longValue = 10000000000L; // L suffix indicates long
float floatValue = 2.5f; // f indicates that 2.5 is a float
double doubleValue = 3.3; // no suffix, double is default
Object objectRef = "hello"; // assign string to an Object reference
System.out.printf("char array = %s\n", String.valueOf(charArray));
System.out.printf("part of char array = %s\n", String.valueOf(charArray, 3, 3));
System.out.printf("boolean = %s\n", String.valueOf(booleanValue));
System.out.printf("char = %s\n", String.valueOf(characterValue));
System.out.printf("int = %s\n", String.valueOf(integerValue));
System.out.printf("long = %s\n", String.valueOf(longValue));
System.out.printf("float = %s\n", String.valueOf(floatValue));
System.out.printf("double = %s\n", String.valueOf(doubleValue));
System.out.printf("Object = %s\n", String.valueOf(objectRef));
}
}
String: valueOf(char[] data)
public class Main {
public static void main(String[] arg) {
char[] ch = {"a","b","c","d"};
System.out.println(String.valueOf(ch));
}
}
String: valueOf(char[] data, int offset, int count)
public class Main {
public static void main(String args[]) {
char charArray[] = { "a", "b", "c", "d", "e", "f" };
boolean booleanValue = true;
char characterValue = "Z";
int integerValue = 7;
long longValue = 10000000000L; // L suffix indicates long
float floatValue = 2.5f; // f indicates that 2.5 is a float
double doubleValue = 3.3; // no suffix, double is default
Object objectRef = "hello"; // assign string to an Object reference
System.out.printf("char array = %s\n", String.valueOf(charArray));
System.out.printf("part of char array = %s\n", String.valueOf(charArray, 3, 3));
System.out.printf("boolean = %s\n", String.valueOf(booleanValue));
System.out.printf("char = %s\n", String.valueOf(characterValue));
System.out.printf("int = %s\n", String.valueOf(integerValue));
System.out.printf("long = %s\n", String.valueOf(longValue));
System.out.printf("float = %s\n", String.valueOf(floatValue));
System.out.printf("double = %s\n", String.valueOf(doubleValue));
System.out.printf("Object = %s\n", String.valueOf(objectRef));
}
}
String: valueOf(double d)
public class Main {
public static void main(String args[]) {
char charArray[] = { "a", "b", "c", "d", "e", "f" };
boolean booleanValue = true;
char characterValue = "Z";
int integerValue = 7;
long longValue = 10000000000L; // L suffix indicates long
float floatValue = 2.5f; // f indicates that 2.5 is a float
double doubleValue = 3.3; // no suffix, double is default
Object objectRef = "hello"; // assign string to an Object reference
System.out.printf("char array = %s\n", String.valueOf(charArray));
System.out.printf("part of char array = %s\n", String.valueOf(charArray, 3, 3));
System.out.printf("boolean = %s\n", String.valueOf(booleanValue));
System.out.printf("char = %s\n", String.valueOf(characterValue));
System.out.printf("int = %s\n", String.valueOf(integerValue));
System.out.printf("long = %s\n", String.valueOf(longValue));
System.out.printf("float = %s\n", String.valueOf(floatValue));
System.out.printf("double = %s\n", String.valueOf(doubleValue));
System.out.printf("Object = %s\n", String.valueOf(objectRef));
}
}
String: valueOf(float f)
public class Main {
public static void main(String args[]) {
char charArray[] = { "a", "b", "c", "d", "e", "f" };
boolean booleanValue = true;
char characterValue = "Z";
int integerValue = 7;
long longValue = 10000000000L; // L suffix indicates long
float floatValue = 2.5f; // f indicates that 2.5 is a float
double doubleValue = 3.3; // no suffix, double is default
Object objectRef = "hello"; // assign string to an Object reference
System.out.printf("char array = %s\n", String.valueOf(charArray));
System.out.printf("part of char array = %s\n", String.valueOf(charArray, 3, 3));
System.out.printf("boolean = %s\n", String.valueOf(booleanValue));
System.out.printf("char = %s\n", String.valueOf(characterValue));
System.out.printf("int = %s\n", String.valueOf(integerValue));
System.out.printf("long = %s\n", String.valueOf(longValue));
System.out.printf("float = %s\n", String.valueOf(floatValue));
System.out.printf("double = %s\n", String.valueOf(doubleValue));
System.out.printf("Object = %s\n", String.valueOf(objectRef));
}
}
String: valueOf(int i)
public class Main {
public static void main(String args[]) {
char charArray[] = { "a", "b", "c", "d", "e", "f" };
boolean booleanValue = true;
char characterValue = "Z";
int integerValue = 7;
long longValue = 10000000000L; // L suffix indicates long
float floatValue = 2.5f; // f indicates that 2.5 is a float
double doubleValue = 3.3; // no suffix, double is default
Object objectRef = "hello"; // assign string to an Object reference
System.out.printf("char array = %s\n", String.valueOf(charArray));
System.out.printf("part of char array = %s\n", String.valueOf(charArray, 3, 3));
System.out.printf("boolean = %s\n", String.valueOf(booleanValue));
System.out.printf("char = %s\n", String.valueOf(characterValue));
System.out.printf("int = %s\n", String.valueOf(integerValue));
System.out.printf("long = %s\n", String.valueOf(longValue));
System.out.printf("float = %s\n", String.valueOf(floatValue));
System.out.printf("double = %s\n", String.valueOf(doubleValue));
System.out.printf("Object = %s\n", String.valueOf(objectRef));
}
}
String: valueOf(long l)
public class Main {
public static void main(String args[]) {
char charArray[] = { "a", "b", "c", "d", "e", "f" };
boolean booleanValue = true;
char characterValue = "Z";
int integerValue = 7;
long longValue = 10000000000L; // L suffix indicates long
float floatValue = 2.5f; // f indicates that 2.5 is a float
double doubleValue = 3.3; // no suffix, double is default
Object objectRef = "hello"; // assign string to an Object reference
System.out.printf("char array = %s\n", String.valueOf(charArray));
System.out.printf("part of char array = %s\n", String.valueOf(charArray, 3, 3));
System.out.printf("boolean = %s\n", String.valueOf(booleanValue));
System.out.printf("char = %s\n", String.valueOf(characterValue));
System.out.printf("int = %s\n", String.valueOf(integerValue));
System.out.printf("long = %s\n", String.valueOf(longValue));
System.out.printf("float = %s\n", String.valueOf(floatValue));
System.out.printf("double = %s\n", String.valueOf(doubleValue));
System.out.printf("Object = %s\n", String.valueOf(objectRef));
}
}
String: valueOf(Object obj)
public class Main {
public static void main(String args[]) {
char charArray[] = { "a", "b", "c", "d", "e", "f" };
boolean booleanValue = true;
char characterValue = "Z";
int integerValue = 7;
long longValue = 10000000000L; // L suffix indicates long
float floatValue = 2.5f; // f indicates that 2.5 is a float
double doubleValue = 3.3; // no suffix, double is default
Object objectRef = "hello"; // assign string to an Object reference
System.out.printf("char array = %s\n", String.valueOf(charArray));
System.out.printf("part of char array = %s\n", String.valueOf(charArray, 3, 3));
System.out.printf("boolean = %s\n", String.valueOf(booleanValue));
System.out.printf("char = %s\n", String.valueOf(characterValue));
System.out.printf("int = %s\n", String.valueOf(integerValue));
System.out.printf("long = %s\n", String.valueOf(longValue));
System.out.printf("float = %s\n", String.valueOf(floatValue));
System.out.printf("double = %s\n", String.valueOf(doubleValue));
System.out.printf("Object = %s\n", String.valueOf(objectRef));
}
}
== vs equals
/*
* Output:
Hello equals Hello -> true
Hello == Hello -> false
* */
public class MainClass {
public static void main(String args[]) {
String s1 = "Hello";
String s2 = new String(s1);
System.out.println(s1 + " equals " + s2 + " -> " + s1.equals(s2));
System.out.println(s1 + " == " + s2 + " -> " + (s1 == s2));
}
}