'New' keyword
Author |
Message |
Yumikage
|
Posted: Thu Sep 18, 2014 1:35 pm Post subject: 'New' keyword |
|
|
I heard Java doesn't have pointers, but I've seen the 'New' keyword being used when I was learning to get user input in Java.
What does it do in Java?
And is using the Scanner class a good way to get input through a console application?
It seems more complicated than what I had to do in C++. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Raknarg
|
Posted: Thu Sep 18, 2014 2:57 pm Post subject: RE:\'New\' keyword |
|
|
the new keyword is used to initialize Objects and arrays in Java. You can make a variable as a Rectangle type, but until you say rect = new Rectangle() it's basically an empty variable.
The scanner works fine.
And yes, Java in a way is a little more complicated than C (but in some ways, much more simple. Depends on what you're doing, really), but that's because C is very very low level. You have lot's of stuff covered for you in Java that you don't need to worry about. |
|
|
|
|
|
Tony
|
Posted: Thu Sep 18, 2014 3:43 pm Post subject: Re: 'New' keyword |
|
|
Yumikage @ Thu Sep 18, 2014 1:35 pm wrote: I heard Java doesn't have pointers, but I've seen the 'New' keyword
There are still references. A common mistake that illustrates this is that in Java
code: |
if("foo" == "bar"){...}
|
will compare memory address, of the two strings, not their value (.Equals() is used for comparing actual content). |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
DemonWasp
|
Posted: Thu Sep 18, 2014 4:39 pm Post subject: RE:\'New\' keyword |
|
|
The new keyword doesn't necessarily imply pointers, and in Java it necessarily does not imply pointers. You'll also find that Java has a NullPointerException, which may be a misnomer. There's a long-standing, exhausting argument over whether Java uses pointers or references.
The Scanner class is pretty decent, but there's also Console for some edge cases. If you want something more powerful, there are libraries like JLine that given you the ability to do some really cool stuff (but of course they're way more complicated).
I'm not sure what you mean by "[Scanner] seems more complicated than what I had to do in C++". |
|
|
|
|
|
Raknarg
|
Posted: Thu Sep 18, 2014 4:42 pm Post subject: RE:\'New\' keyword |
|
|
By more complicated, scanf() and a char array is much simpler to use than what you do in Java |
|
|
|
|
|
Yumikage
|
Posted: Thu Sep 18, 2014 6:20 pm Post subject: RE:\'New\' keyword |
|
|
Nevermind about scanners being complicated.
Pretend I didn't say anything abou thtat lol.
I always thought the new keyword was used for allocating memory for pointers. I guess I'll have to look at that again.
I read that Java doesn't have pointers though, just references (which aren't the same as references in C++). Can you confirm this? |
|
|
|
|
|
DemonWasp
|
Posted: Thu Sep 18, 2014 8:46 pm Post subject: RE:\'New\' keyword |
|
|
scanf() is dangerous from a security perspective though -- and format specifiers are actually pretty complex.
Keywords can have different meaning between languages, and "new" is certainly among them.
From a C++ perspective, Java has something like "nullable references" -- they don't have the flexibility (or danger) of pointers and they don't support pointer arithmetic, but they do allow for nullability and aliasing and pass-by-reference semantics and so forth. Java does not have pointers. |
|
|
|
|
|
Insectoid
|
Posted: Thu Sep 18, 2014 9:24 pm Post subject: RE:\'New\' keyword |
|
|
Iirc fscanf is secure and functions just like scanf if you pass stdin as the file. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
|
|