r/javahelp • • 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?

48 Upvotes

18 comments sorted by

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.

15

u/[deleted] Nov 04 '19

Ok thanks guys. That makes sense. Getting slightly ahead of myself but was just curious

22

u/bwz3r Nov 04 '19

always get ahead of yourself. otherwise, you're never really in the lead.

3

u/Orbitaliser Nov 04 '19

Very well said

2

u/[deleted] Nov 05 '19

Just a tip, if you ever see something with a capital in the beginning it is a class. One more thing, do take a look into wrapper classes for primitive types. If you really want to get better, do join check the code in java builtin classes or libraries. Like for example how does str.length works, go into the String lib and understand it.

22

u/firsthour Profressional developer since 2006 Nov 04 '19

char (character) is also a primitive. String can basically be considered a wrapper for a char array. In fact, the first lines of the String class are:

/** The value is used for character storage. */
private final char value[];

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 final variables) 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

u/[deleted] 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

u/[deleted] 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 like Integer and 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

u/Orbitaliser Nov 05 '19

Yes, apologies. Forgot to discuss that.

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

u/[deleted] 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: boolean

  • 8 bits: byte (signed - different to other programming languages - range -128 to +127)

  • 16 bits: short and char (unsigned)

  • 32 bits: int and float

  • 64 bits: long and double

From small to big rather. boolean is the smallest, double the largest.

1

u/[deleted] Nov 05 '19

[removed] — view removed comment

2

u/desrtfx Out of Coffee error - System halted Nov 05 '19

Needs more coffee ;)