Spaces:
Running
Running
File size: 1,611 Bytes
409c5b6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
Java has variables of reference type.
In a reference type variable, the variable is placed in reference to the actual value.
String b = "bye!";
- immutable
- but can point to another string
================
String name = "Jack Java";
String address = "Java rd. " + "64" + ", 12345, Javatown";
String phonenum = "040-" + (12345 * 23456); //multiply, then concat as strings
System.out.println(name);
System.out.println(address);
System.out.println(phonenum);
Program outputs:
Jack Java
Java rd. 64, 12345, Javatown
040-289564320
================
String length - .length()
String first = "Hey";
System.out.println(first.length());
System.out.println("Hello everyone".length());
int length = (first + "!!!").length();
System.out.println(length);
Program outputs:
3
14
6
================
single characters - charAt(i)
Scanner reader = new Scanner(System.in);
System.out.print("Give a string: ");
String str = reader.nextLine();
for (int i=0; i<str.length(); i++) {
System.out.println(str.charAt(i));
}
Program outputs:
Give a string: Test
T
e
s
t
// returns a 'character' - which is a number type
char first = 'O';
char second = 'k';
System.out.println(first + second);
Program outputs:
186
// A character can be converted into a string
// 1 either by using the method toString in the Character class
char cr = 'X';
String str = Character.toString(cr);
Program outputs:
X
// 2 or usually more easily by CONCATenating a character with an EMPTY STRING:
char first = 'O';
char second = 'k';
System.out.println("" + first + second);
Program outputs:
Ok
================ |