Java Variables

[S1E2]

Introduction

// datatype variableName
int age;
double salaryRequirement;
boolean isEmployed;
age = 85;
int yearCodecademyWasFounded = 2011;

ints

// int variable declaration
int yearJavaWasCreated;
// assignment
yearJavaWasCreated = 1996;
// declaration and assignment
int numberOfPrimitiveTypes = 8;

doubles

// doubles can have decimal places:
double price = 8.99;
// doubles can have values bigger than what an int could hold:
double gdp = 12237700000;

doubles

// doubles can have decimal places:
double price = 8.99;
// doubles can have values bigger than what an int could hold:
double gdp = 12237700000;

booleans

boolean javaIsACompiledLanguage = true;
boolean javaIsACupOfCoffee = false;

char

char grade = 'A';
char firstLetter = 'p';
char punctuation = '!';

String

String greeting = "Hello World";

Static Checking

int greeting = "Hello World";
error: incompatible types: String cannot be converted to int
int greeting = "Hello World";

Naming

String data = "Delilah";
String nameOfUser = "Delilah";
// good style
boolean isHuman;
// bad styles
// no capitalization for new word
boolean ishuman;
// first word should be lowercase
boolean IsHuman;
// underscores don't separate words
boolean is_human;

Review of this lesson

  • int, which stores whole numbers.
  • double, which stores bigger whole numbers and decimal numbers.
  • boolean, which stores true and false.
  • char, which stores single characters using single quotes.
  • String, which stores multiple characters using double quotes.
  • Static typing, which is one of the safety features of Java.
  • Variable naming conventions.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store