4

I'm new to Java, but understand C++.

string foo;

When I do if(foo.charAt(i) == 'a')

How is 'a' a reference?

And how foes foo.ChaAt('a') return a reference? When I debug it looks just like 'a'?

Are these pointers? Am I just too drunk?

How would these look if they returned values?

CodingIsAwesome
  • 1,946
  • 7
  • 36
  • 54
  • You need to provide a little more context around what you are doing? What is `foo`? What is the rest of your code? What do you expect? What are you actually getting? – jbranchaud Feb 05 '12 at 03:57
  • @Ernest Friedman-Hill: Sure but he mentioned he was new to Java, so I +1'ed the answer mentioning the word *"autoboxing"* because that can be incredibly confusing. Say, *Character[] array = { "a".charAt(0) }* will compile fine and put a **reference** into the array. Oh, I know (I've got my SCJP since the last century), I know, it's a *char* that is returned by *charAt*. Yet you don't put primitives in an array. So here comes autoboxing to confuse newcomers ; ) – TacticalCoder Feb 05 '12 at 04:07

6 Answers6

4

Sounds like you've been the victim of some misinformation. The method String.charAt() returns a char by value. It does not return a reference of any kind; there are no pointers involved.

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
  • Maybe... I'm just trying to learn on my own. I read that '==' was only for comparing if things were the same object or reference. Like if char was char. – CodingIsAwesome Feb 05 '12 at 18:30
  • You use `==` for primitive values, too. – Louis Wasserman Feb 05 '12 at 18:33
  • "==" checks whether the actual bits in the variable are the same. In the case where a variable contains a reference to an object, you're testing whether two variables refer to the same object. In other cases, you're testing whether the bits that make up the value are the same. – Ernest Friedman-Hill Feb 05 '12 at 19:40
3

Although some alcohol might be involved I'll try to give you a fast basic introduction to Java! ;)

Before we even start, keep in mind that in Java there are NO pointers. Now lots of people will start yelling at me but it's the way a say and I'm ready for any possible comments on this!

  • In Java there are Objects and primitive data types. byte, short, int, long, float, double, boolean, char are the only primitives; everything else in an Object (Arrays are Objects too)
  • A variable in Java can hold either the exact value of the primitive data type or a value that tells the JVM how to get to the Object (note that I just could have said reference but I didn't 'cause it's better like this)
  • A String in Java is an Object (a special one because it's immutable and there is a lot of talking that can be done about String pool and interning) that is backed by an array of chars
  • foo.charAt(i) returns the char (a primitive data type) at the given zero-based index i!
  • 'a' is a char! That's it...
  • The == operator compares variables. Variables can again have a primitive or Object type. In your case (primitives) it compares 2 chars and returns either true or false if they have the same value. You can also compare different primitive types together with the same operator but it gets a little more complicated because of their different sizes. You can also compare Objects variables (references) with that but note that in that case you're comparing the 2 values that tell the JVM how to get to the Objects. (I.E. You can only determine if the two variables get to the same exact Object, the same one!)

For more clarification on pass-by-reference (that does not exist in Java at all) you can look at this question

Let me know if you need any other clarification

Community
  • 1
  • 1
Marsellus Wallace
  • 17,991
  • 25
  • 90
  • 154
2

In Java everything, except for primitive types are references. String is not considered a primitive type, but char is. So, AFAIK 'a' in your example is not a reference.

However, Java supports autoboxing, so if you try to use 'a' as an object it will probably work just fine, as if it were a reference.

Update: some examples may help here:

char a = 'a';
char b = 'a';
System.out.println(a == b); // true
String c = "a";
String d = "a";
System.out.println(c == d); // Dunno; may be true if the compiler created a single object for c and d, otherwise it's false
String e = new String("a");
String f = new String("a");
System.out.println(e == f); // false
mgibsonbr
  • 21,755
  • 7
  • 70
  • 112
  • 1
    And to add to that: there may be a difference between primitive and reference types, both are passed-by-value, no exceptions. – M Platvoet Feb 05 '12 at 10:28
  • Wait so primitive types are passed by reference so I can use == to compare reference values? – CodingIsAwesome Feb 05 '12 at 18:25
  • I think you're confusing the concepts of ["passing by reference"](http://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_reference) and ["reference types"](http://en.wikipedia.org/wiki/Reference_%28computer_science%29). The explanation is too long to fit here, but the other answerers have some good insight on the matter. – mgibsonbr Feb 05 '12 at 18:50
2

Assumption: foo is of type String

How is the return of foo.charAt(i) a reference?

It's not reference. it's primitive type: char

Check the java doc for charAt

See the return type: it's char. char is not a reference type.

In Java, you have a reference type and primitive type.

Azodious
  • 13,752
  • 1
  • 36
  • 71
1

charAt doesn't return a reference to an object, it returns a char primitive type. When you use == to compare two chars for equality, you're comparing their values, not their references (in Java, we don't talk about pointers).

Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • 1
    So I can compare primitive types with '==' but not say a string? Because one is primitive? So '==' is now comparing values when I compare two chars, but with I compare two strings '==' is like hey these two are not primitive so no go? – CodingIsAwesome Feb 05 '12 at 18:29
  • @CodingIsAwesome exactly! `'a' == 'a'` no matter where 'a' came from, but `"a" == "a"` if and only if both sides refer to the same object. See my updated answer for more details. – mgibsonbr Feb 05 '12 at 18:59
1

'a' will not be a reference. foo.charAt(index) will return a char primitive, so you can use == safely. Otherwise you would have to use foo.charAt(index).equals(bar).

rtheunissen
  • 7,347
  • 5
  • 34
  • 65
  • I don't believe foo.charAt(i).equals(bar) will work. I think I tried this and it told me that there was no .equals associated to a .charAt() – CodingIsAwesome Feb 05 '12 at 18:26
  • Yeah I know, I'm saying that IF it was a reference type you would have to use `.equals`, but because it's a primitive type you can use `==` safely. – rtheunissen Feb 06 '12 at 00:26