In previous notes, we mentioned that the bits and bytes on a computer can be interpreted in lots of different ways.
One of the ways our computer programs know how to interpret a byte (or more) of data in the context of a programming language is by the type of the data, which is set in the program. Every variable in a program can hold data of a specific type.
In a program, there are three types of things that have types:
2
or "abc"
that are used only once and represent a value directly.General Type | Languague-specific usage | Description |
---|---|---|
Boolean | TypeScriptlet boo:boolean=true; java boolean boo=false; |
The value of a boolean is either true or false . The simplest data type! |
Numeric | TypeScriptlet n:number=1; let x:number=3.21; java int n=11; double x=1.2; |
Holds a number. In TypeScript and javascript, all numbers are floating point numbers (meaning they can be integers or decimals) while java separates decimals (called doubles for historical reasons) from integers. There are advantages to both systems. |
Characters and Strings | Typescriptlet k:string="word"; java char c='a'; String s="12"; |
A character is a single piece of text data, ready to print to the screen, such as the character 'a' or '8' or 'â' or even '😀' .If you combine more than one character, you get a string, like "string83" . In javascript / TypeScript, characters are just small strings, but in java they are separate types of data with different rules. Strings and characters are always surrounded by quotation marks. |
Arrays | TypeScriptlet a:number[]=[1, 2, 3]; java boolean[] a={true, false}; |
A collection of more than one item of any type (even other arrays!). type[] in either language creates a group that can hold many values of the given type. |
Objects | Much more later! | A group of many related values and functions that work together as one larger thing. The center of object-oriented programming. Objects often have their own special names. |
What type of data is the value 234
in TypeScript? What is it in java?
Typescript: number
java: int
You want to write the code j = "bob"
. What data type must j
be declared as for this to not give an error?
1
string
The following TypeScript code gives an error. Why?
1
2
let j:string;
j = 123;
j
is a string but you are trying to assign it a number
The following java code gives an error. Why?
1
2
int j;
j = "123";
j is an integer, but you are assigning it a string.
You write the following code:
1
let b = (2 + 2 > 4);
What do you think is the VALUE and TYPE of b? (cool thing to try: you can check your answer by typing “ts-node” on your command line, then typing that code. After pressing enter, type b
to output its value. Type .exit
(notice the period!) to exit.)
b is a boolean, with the value false
Create flash cards or a quizlet deck with the following words, with examples, a description, and a code example for TypeScript and java on the back.
We have a quizlet class at https://quizlet.com/join/S8XGx3k93 and you can add your decks there if you make them, but you REALLY should make your own deck before using those of others. You learn as much from making the flashcards as using them, if not more.