Java How is foo.charAt (i) link returned?

I am new to Java but understand C ++.

string foo;

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

How is the 'a' link?

And how do foo.ChaAt ('a') enemies return the link? When I debug, does it look the same as "a"?

Are these pointers? Am I too drunk?

What would they look like if they returned the values?

+4
source share
6 answers

Although some kind of alcohol may be involved, I will try to give you a brief basic introduction to Java!;)

Before we get started, keep in mind that there are no pointers in Java. Now many people will start yelling at me, but this is a way of saying, and I am ready for any possible comments on this subject.

  • Java has objects and primitive data types. byte, short, int, long, float, double, boolean, char - the only primitives; everything else in the object (arrays are also objects)
  • A variable in Java can contain either the exact value of a primitive data type or the value that tells the JVM how to get to the object (note that I could just reference this link, but I didn’t do it better, this)
  • A string in Java is an object (special because it is immutable, and there is a lot of talk that can be done about string pooling and interning) that is supported by an array of characters
  • foo.charAt(i) returns a char (primitive data type) at the specified index with a value of zero i !
  • 'a' is char ! What is it...
  • The == operator compares variables. Variables can again have a primitive or an object type. In your case (primitives) it compares 2 characters and returns either true or false if they have the same value. You can also compare different primitive types with the same operator, but it gets a little more complicated due to their different sizes. You can also compare object variables (links) with them, but note that in this case you are comparing 2 values ​​that tell the JVM how to get to the objects. (IE you can only determine if two variables fall into the same exact object, the same!)

For more information on pass-by-reference (which doesn't exist at all in Java) you can look at this question

Let me know if you need any other clarification.

+2
source

You seem to be the victim of some kind of misinformation. The String.charAt() method returns a char value by value. It does not return any reference; no pointers.

+4
source

In Java, all but primitive types are references. String not considered a primitive type, but char is. So AFAIK 'a' in your example is not a link.

However, Java does support autoboxing , so if you try to use "a" as an object, it will probably work just as if it were a link.

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 false String e = new String("a"); String f = new String("a"); System.out.println(e == f); // false 
+2
source

Assumption: foo is of type String

How is foo.charAt (i) link returned?

This is not a link. this is a primitive type: char

Check java doc on charAt

See return type: it char . char is not a reference type.

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

+2
source

charAt does not return an object reference; it returns a primitive char type. When you use == to compare two characters for equality, you are comparing their values, not their references (in Java we are not talking about pointers).

+1
source

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

+1
source

Source: https://habr.com/ru/post/1394831/


All Articles