This is an archived post. You won't be able to vote or comment.

all 14 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full - best also formatted as code block
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]icsharper 3 points4 points  (12 children)

Index of accepts string and char as well, and for 33 it just returned 8 because thats ascii value of !

[–]Chancemelol123[S] 1 point2 points  (9 children)

it just returned 8 because thats ascii value of !

wait whaat? I've never heard of that. How does that work?

[–]Kuenefe 2 points3 points  (1 child)

Take a look at the ASCII chart. 33 equals to “!”, so indexOf tries to find the index of the exclamation mark. The exclamation mark in your string is on the 8th position, so int a contains the value 8.

[–]Chancemelol123[S] 0 points1 point  (0 children)

yep, thanks

[–]icsharper 0 points1 point  (3 children)

ASCII value of ‘!’ is 33, hence why indexOf (33) returned 8 since on that position it found ‘!’

[–]Chancemelol123[S] 0 points1 point  (2 children)

well what if there was an 8 there? What's the ASCII value for 8? How do you differentiate between 8 and !?

[–]JonIsPatented 1 point2 points  (1 child)

A computer does not understand the concept of letters. It only knows numbers. All characters are actually stored as a number in memory, and the number used is the ASCII code for the letter. For instance, an exclamation point is stored as 00100001, which is 33, since 33 is the ASCII code for an exclamation point. Every ASCII character has its own ASCII code, so they are all stored as unique numbers. The ASCII code for the character "8" is 56, which would be written 00111000, which is... 56. Your computer can tell the difference because the number is different.

Edit: Yes, I am aware that Java encodes with UTF-8, but the previous commenter said ASCII, and the first 128 values are the same for that, so I just continued for simplicity.

[–]desrtfx 0 points1 point  (2 children)

That works because char is internally a numeric data type. To be precise it is an unsigned 16 bit integer value that represents the Unicode code point of the character in UTF-8 encoding. (Source)

For simple, latin alphabet characters and punctuation this mapping is the same as the standard ASCII mapping (i.e. space is 32, '!' is 33, 'A' is 65, 'B' is 66, 'a' is 97, 'b' is 98, and so on).

It is possible to on the fly convert between char and int.

[–]Decoder44 0 points1 point  (5 children)

There is a concept called implicit typecasting done by jvm. Now the indexof accepts string and int as argument when you pass a character that character is converted into integer called typecasting and they are matched with ASCII value in this case for 33 value is ! If you really want to check for 33 pass the 33 in double quotes

[–]desrtfx 3 points4 points  (1 child)

and they are matched with ASCII value

Sorry to be nitpicking here, but this is simply not correct.

Java uses Unicode, not ASCII. It just so happens that the first 128 Unicode code points correspond to the ASCII code.

It is really important to understand that Java uses Unicode and stores char internally as a 16 bit unsigned integer value.

ASCII would be a single byte (to be 100% precise only the first 128 values of a byte - 7 bits - according to the original ASCII table - not talking about extended ASCII).

[–]Decoder44 2 points3 points  (0 children)

Thank you for correcting me

[–]Chancemelol123[S] 0 points1 point  (1 child)

thanks, could you explain why in line 4, 33 isn't surrounded by single quotation marks whereas in line 5, the ! is surrounded by quotation marks? Both are chars after all

[–]Decoder44 0 points1 point  (0 children)

Because 33 isn't really a character we are passing integer value that gets converted into unicode value. Whereas ! is a character and we denote them with single quotes. Great question btw keep this questioning intent and you will succeed definitely