blob: 01cbf18c6d81c8f476108a731accbc47ea4d2139 [file] [log] [blame]
jeffhao5d1ac922011-09-29 17:41:15 -07001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/**
18 * Interned strings
19 */
20public class Main {
21 public static void main(String args[]) {
22 String a, b;
Ian Rogers7dfb28c2013-08-22 08:18:36 -070023 final String foo = "foo";
24 final String bar = "bar";
jeffhao5d1ac922011-09-29 17:41:15 -070025
Ian Rogers7dfb28c2013-08-22 08:18:36 -070026 // Two interned strings should match.
jeffhao5d1ac922011-09-29 17:41:15 -070027 a = foo.concat(bar).intern();
28 b = foo.concat(bar).intern();
29 if (a == b && foo != bar) {
30 System.out.println("good! " + a);
31 } else {
Ian Rogers7dfb28c2013-08-22 08:18:36 -070032 System.out.println("bad! " + a + " != " + b);
33 }
34
35 // An interned string should match a string literal.
36 a = ("f" + foo.substring(1,3)).intern();
37 if (a == foo) {
38 System.out.println("good! " + a);
39 } else {
40 System.out.println("bad! " + a + " != " + b);
41 }
42
43 // Check that a string literal in libcore equals one in the app.
44 a = (new java.nio.charset.IllegalCharsetNameException(null)).getMessage();
45 b = "null";
46 if (a == b) {
47 System.out.println("good! " + a);
48 } else {
49 System.out.println("bad! " + a + " != " + b);
jeffhao5d1ac922011-09-29 17:41:15 -070050 }
51 }
52}