So, I was wondering what the difference was between this:
first = "Hello!"
and:
String first = "Hello!"
So, I was wondering what the difference was between this:
first = "Hello!"
and:
String first = "Hello!"
The former assigns to a declared variable; the latter declares and assigns a variable.
I don't think this:
first="Hello!"
will compile as the compiler will throw an error asking for the type of first. Java is a strongly typed language - each variable needs a well-defined type. I'm ignoring generic types like E for the moment...
String first;Not really sure what you're asking. In your first example: first = "Hello!" you aren't declaring first, so if you run only that line of code, it will not work. Assuming you declared first as a String, then both examples are the same. And there is no primitive string type like there is with int and Integer. String is always an object.
first = "Hello!"
will not compile correctly because it doesn't have a type. In Java, when you create a variable (in this instance called 'first'), you must give it a type such as String, int, long, et cetera. Because the type wasn't given, it doesn't know what to do. So, when you create the variable, you must use String first = "Hello!"
You don't need to give the type when the variable is already declared. For example,
String first = "Hello!"
first = "Goodbye!"
first will now be "Goodbye!"