Java Variables and Datatypes

Learn about datatypes in Java and how we use them. Then, practice your skills with two projects where you create and manipulate variables.

Start[missing "en.views.course_landing_page.learn-java.course_illustration" translation]
Chevron Left Icon
Learn Java: Variables
Lesson 1 of 2
Chevron Right Icon
  1. 1

    Let’s say we need a program that connects a user with new jobs. We need the user’s name, their salary, and their employment status. All of these pieces of information are stored in our program. W…

  2. 2

    The first type of data we will store is the whole number. Whole numbers are very common in programming. You often see them used to store ages, or maximum sizes, or the number of times some code has…

  3. 3

    Whole numbers don’t accomplish what we need for every program. What if we wanted to store the price of something? We need a decimal point. What if we wanted to store the world’s population? That nu…

  4. 4

    Often our programs face questions that can only be answered with yes or no. Is the oven on? Is the light green? Did I eat breakfast? These questions are answered with a boolean, a type that ref…

  5. 5

    How do we answer questions like: What grade did you get on the test? What letter does your name start with? The […] data type can hold any character, like a letter, space, or punctuation mark…

  6. 6

    So far, we have learned primitive data types, which are the simplest types of data with no built-in behavior. Our programs will also use […] s, which are objects, instead of primitives. Object…

  7. 7

    The Java programming language has static typing. Java programs will not compile if a variable is assigned a value of an incorrect type. This is a bug, specifically a type declaration bug. Bug…

  8. 8

    Let’s imagine we’re storing a user’s name for their profile. Which code example do you think is better? […] or […] While both of these will compile, the second example is way more easy t…

  9. 9

    Creating and filling variables is a powerful concept that allow us to keep track of all kinds of data in our program. In this lesson, we learned how to create and print several different datatyp…

  1. 1

    Let’s say we are writing a program that represents a user’s bank account. With variables, we know how to store a balance! We’d use a […] , the primitive type that can hold big decimal numbers….

  2. 2

    In our bank account example from the last exercise, we used […] to add! […] If we wanted to withdraw from the balance, we would use […] : […] Addition and subtraction work with …

  3. 3

    Let’s say that our employer is calculating our paycheck and depositing it to our bank account. We worked 40 hours last week, at a rate of 15.50 an hour. Java can calculate this with the multiplicat…

  4. 4

    If we baked 10 cookies and gave them out in batches of 3, how many would we have leftover after giving out all the full batches we could? The modulo operator […] , gives us the remainder af…

  5. 5

    Now, we’re withdrawing money from our bank account program, and we want to see if we’re withdrawing less money than what we have available. Java has relational operators for numeric datatypes th…

  6. 6

    So how would we validate our paycheck to see if we got paid the right amount? We can use another relational operator to do this. […] will tell us if two variables are equal: […] Notice t…

  7. 7

    How could we make sure we got paid at least the amount we expected in our paycheck? We could use greater than or equal to, […] , or less than or equal to, […] ! […]

  8. 8

    So far, we’ve only been using operations on primitive types. It doesn’t make much sense to multiply Strings, or see if one String is less than the other. But what if we had two users logging into a…

  9. 9

    We have covered a lot of built-in functionality in Java throughout this lesson. We’ve seen […] , […] , […] , […] , and many other operators. Most of these only work on primitives, but s…

  10. 10

    What’s the use of having variables if you can’t do anything with them? We’ve now seen some ways you can operate on variables and compare them. The possibilities are endless! We covered: - Addition…

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo

Java Variables and Datatypes

Start[missing "en.views.course_landing_page.learn-java.course_illustration" translation]