r/javahelp • u/[deleted] • Nov 04 '19
Why is String capitalised, but int, float, boolean etc are not?
Is it because it's a reference type? If so, are all reference types in that format and all primitive types are lower case?
19
u/desrtfx Out of Coffee error - System halted Nov 04 '19
If so, are all reference types in that format
The Java code conventions stipulate:
Class names (which make reference types) use PascalCase
variable and method names use camelCase (same as PascalCase only with a lowercase first letter)
method names usually use a <verb><noun> (or <adjective><noun>) naming pattern
Constants (
static finalvariables) use UPPER_SNAKE_CASE
Primitive data types always start with a lowercase letter (int, char, boolean, double, float, short, long). Don't confuse them with their wrapper counterparts Integer, Character, Boolean, Double, Float, Short, Long - they serve a completely different purpose.
String is quite special in Java. It is a reference type because String is a Java library class. For convenience, the Java language designers have built a couple things that you can normally only do with primitives, though: assignment with = "hello" instead of = new String("hello") (which is also valid, btw), conatenation with + -> "hello" + " " + "world"(even though a StringBuilder is better suited for repeatable concatenation). Still, one common pitfall is that beginners try to compare String values with == or !=. Since String is a reference type .equals or .equalsIgnoreCase must be used.
String has another peculiarity: it is immutable. Once a string value is assigned to a variable of type String, it cannot be changed. Re-assignment is possible, but this creates an entirely different, new string. That's also the reason why StringBuilder (as mentioned above) is better for string concatenation.
7
Nov 04 '19
Wow thanks a lot. We've only just touched on concatenation in the book I'm using. Had never heard of the term "wrapper" before. On Chapter 4 of 24 so far, so it's early days. Sure it'll all make sense with practice. Really enjoying it so far. Thanks again for the detailed info
3
Nov 05 '19
A wrapper is when a single item is contained in a class, effectively.
Integer is a wrapper class for int; you can't call methods on ints, but because Integer is an object, you can call all the methods you want.
2
u/morhp Professional Developer Nov 04 '19
String has another peculiarity: it is immutable.
That's not really special, so are
File,Duration,LocalDate,Color, (regexp)Pattern, wrapper classes likeIntegerand many more.Re-assignment is possible, but this creates an entirely different, new string.
That's a confusing statement.
String foo = "Hello"; foo = "World";creates no new Strings apart from the obvious 2.
2
u/Orbitaliser Nov 04 '19 edited Nov 04 '19
Correct. It is a reference type, like all objects in Java.
A very special reference type, since unlike your average object:
Object object = new Object();
String can be declared as
String str = "Foo";
Although the standard object declaration works too.
It's good that it looks like you are already getting used to the idea of variables referring to objects rather than holding the objects themselves.
Other comments on this thread have mentioned immutability in Strings which is a pretty interesting thing to look up in general. You'll probably come across defensive programming as well.
2
u/Shunpaw Nov 05 '19
I want to make sure there is no confusion: If you do not make a String foo = new String; and declare two strings you can either end up with one or two objects depending on what they are called.
If you want two different objects, call new String;
1
2
u/PrinceValkyr Nov 04 '19
int, float, and boolean are primative types
String is a class that someone else wrote, and named it String.
3
Nov 05 '19 edited Nov 05 '19
[removed] — view removed comment
4
u/desrtfx Out of Coffee error - System halted Nov 05 '19 edited Nov 05 '19
From big to small (in memory size, some are the same size)
1 bit:
boolean8 bits:
byte(signed - different to other programming languages - range -128 to +127)16 bits:
shortandchar(unsigned)32 bits:
intandfloat64 bits:
longanddoubleFrom small to big rather. boolean is the smallest, double the largest.
1
64
u/MyNameIsRichardCS54 Nov 04 '19
String is an object, int, float etc are primitives. There are also Integer, Float etc which are wrappet objects for the primitive types.