Data Types


Collapse Content

Basic Types

As mentioned in a previous node, when you declare a variable in Java, you need to also state what type it is, i.e. what kind of data it can hold. There are 8 primitive data types in Java, but these are the main 4:

type definition example operation //result
int This holds integers with values from about -2 billion to +2 billion. This is what we've been dealing with until now. int sum = 999 + 1; //1000
double This holds decimal numbers (real numbers) with a wide range, like -2.1 or 123.456789. double sum = 1+0.23 //1.23
- boolean This can only hold two values: true or false. boolean greater = (7 > 10) //false
char This holds single characters, like 'c' or 'ß' or '1' in character form. To create a character you need to use single quotes. char letter = 'z';

String

In addition, there's the important String type, which is a little different than the above types, but is also built-into the language. A String holds any number of characters together, so it is used to store any text, like words and sentences. You can create a String by using double-quotes: String str ="hello";. Note that it is written as String (with a capital "S") unlike the simple types above. This is some sample code with String:

  • String name = "Jim"; Simply store the string "Jim" in the variable name.
  • String num = 23; ERROR! You cannot store a number in a String!
  • String num = "23" ...But you can store a String representation of a number.

In addition, you can concatenate characters with Strings or String with each other to make larger Strings.* For example:

  • String alpha = "ab" + 'c'; combines the character with a String to make a larger String, "abc".
  • String sentence = "Pack " + "my " + "box."; will set sentence to "Pack my box."

You can even concatenate numbers with Strings, and they will automatically be turned into Strings:

int n = 5;
String message = "Pick a number from 1 to " + n;
//message is now:  "Pick a number from 1 to 5"

Soon you'll see how to print Strings. Meanwhile, see if you can handle different types in the following problem:

Challenge

You will be given an int a and a double b as input. You need to calculate their sum and then return a String message which says:
"The sum is [sum here]"
(Place the correct sum in the string instead of the brackets.)

* However, don't use + to combine characters together with each other, since they'll 'turn into' numbers. This will be covered in a later node.

Challenge

Return an exact sentence with the sum, as specified.

Please sign in or sign up to submit answers.

Alternatively, you can try out Learneroo before signing up.

Comments

  • It doesn't seem to compile my answer but it runs on Eclipse just fine.
    Here is my code

    public class DataTypes {

    public static void main(String[] args) {
    
            int a = 2;
            double b = 7.5;
            double sum = a + b;
            System.out.println("The sum is" +  sum);
    
    }
    

    }

  • @William the only class that can be public is Main. So either remove 'public' or rename the class to 'Main'.

  • First getting an error while compiling about return statement missing, wheb added about incompatible types - required String, found double
    Here the code:

    cont...
  • @Honzis, you need to return a String that contains the correct message. So remove return sum and return message instead.

  • Thanks for the quick answer, got it now. :)

  • What is the code to solve this

  • @pat, I added an explanation, which you can 'cheat' to view.

  • Don't system.out.print, return.

  • @William, in this question You should implement code in doStuff() method and return the message. my code

  • @Honzis , You should return only a string type in doStuff() method. because return type of doStuff method is string, check my code :) my code

  • Or you could cast a double on the sum my code

  • Just FYI, I had originally put my string as "The sum is: " + sum. It said it was incorrect because I put the : in. So, even though it ran fine, gave the correct answer, etc., it showed incorrect

    cont...
  • import java.util.Scanner;
    public class DataTypes
    {
    public static void main(String[] args)
    {
    int a;
    double b;
    Scanner input = new Scanner(System.in);

    cont...
  • The exercise is to get you to understand how to attach a variable into a string, over complicating the output or process only clouds the issue. keep it simple like this...

    String answer = "The sum is " + (a+b);
    return answer;

All Node Comments
Contact Us
Sign in or email us at [email protected]