r/learnjava • u/Tasty_Escape4549 • Sep 21 '24
What does putting . do?
I realize this sounds dumb but ive been learning java and gotten pretty far but never learned what . does. e.g
String s = args[0]
char c = s.charAt(0)
what does the . in s.charAt(0) do?
16
u/law_rnz Sep 21 '24
its called the dot operator and is commonly used in many programming languages to access properties/methods from an object (you can also access static members by referring to it's class name)
1
3
u/Agni_1999 Sep 22 '24
The . is an operator (dot operator). It basically has 2 jobs: to tell the compiler 'where' to call a method from and 'on which object' to call it on. For eg:
import java.util.Scanner
In this case, the dot operator signifies a kind of a hierarchy and tells the compiler 'where' to load the class from (the Scanner class from inside the util package from inside the java package). Think of it kind of like your computer's file path to the Documens folder: C:/Users/Username/Documents.....and so on, but instead of the '/' it's a dot. Some packages and classes are already preloaded into the compiler so we don't need to explicitly call them using the import statement and in such cases, the work of the dot operator doesn't seem as significant, but when you work with custom-made classes and packages which are not preloaded into the compiler, that's when the dot operator becomes useful.
2) char c = s.charAt[3]
Here the . operator tells the compiler 'on which object' to use the charAt[] method on (in this case, the string 's' so that the character in the index no. 3 can be returned).
1
u/AutoModerator Sep 21 '24
It seems that you are looking for resources for learning Java.
In our sidebar ("About" on mobile), we have a section "Free Tutorials" where we list the most commonly recommended courses.
To make it easier for you, the recommendations are posted right here:
MOOC Java Programming from the University of Helsinki
accompanying site CaveOfProgramming
accompanying site NewThinkTank
Hyperskill is a fairly new resource from Jetbrains (the maker of IntelliJ)
Also, don't forget to look at:
If you are looking for learning resources for Data Structures and Algorithms, look into:
"Algorithms" by Robert Sedgewick and Kevin Wayne - Princeton University
Coursera course:
Coursebook
Your post remains visible. There is nothing you need to do.
I am a bot and this message was triggered by keywords like "learn", "learning", "course" in the title of your post.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
1
u/IsThisWiseEnough Sep 24 '24
I assume with this question Java is your first pl. and there are still to much way to go. Also asking this means you better learn the programming paradigms as well. Nothing to humiliate but to show direction, curiosity is the exact opposite of ignorance.
-1
u/Stay_Silver Sep 21 '24
Yikes
3
u/Bright-Historian-216 Sep 21 '24
The only reason for it to seem like a stupid question is that most of us just never thought about that
0
u/Stay_Silver Sep 21 '24
i say yikes not due to the question being / seeming stupid. I understand stupid questions can happen for new coders. OP want to learn java has google and many other engines but goes to reddit. It also shows lack of coding and attempting / practice coding in java
2
u/Tasty_Escape4549 Sep 21 '24
Im learning AP Comp sci rn and my teacher is extremely awful. He skims over many topics and expects me to know some of them already. So that's why I somewhat understand java but don't know simple things like what the dot does. I came to reddit because I know some people can put it in terms I understand, which they have. Theres no need to be rude, I've just started learning and people go at different paces
0
Sep 21 '24
[deleted]
1
u/Stay_Silver Sep 21 '24
I googled op's post. Here is it's ai answer The dot (
.) ins.charAt(0)is used to access a specific method or property of an object. In this case,sis an object of typeString, andcharAtis a method of theStringclass.The
charAtmethod takes an integer argument (in this case, 0) and returns the character at that index in the string. So,s.charAt(0)will return the character at the very beginning of the strings.Seems pretty clear to me op did not google
3
1
u/Bright-Historian-216 Sep 21 '24
If your browser has ai, then of course he's going to find it immediately. It's surreal to think about how it was impossible just a month ago.
2
u/Stay_Silver Sep 21 '24
no i have a brain and am not lazy it is easy enough to navigate to google's ai gemini. You share op's problem. u/bye
1
u/nekokattt Sep 21 '24 edited Sep 22 '24
I mean, I legit just typed into google "java what does the dot do" and it was the first result.
Edit: 4 duplicate responses from me... reddit timing out again and not implementing basic message deduplication.
-1
u/nuttwerx Sep 21 '24
The troubling point is that op claims to have gotten pretty far in his learning voyage but yet doesn't understand what the dot operator stands for
-2
u/nuttwerx Sep 21 '24
The troubling point is that op claims to have gotten pretty far in his learning voyage but yet doesn't understand what the dot operator stands for
2
•
u/AutoModerator Sep 21 '24
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:
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.