<jupyter_start><jupyter_text>Basic TypesJava has two kinds of type,- primitive types that are directly mapped to CPU basic types- reference types that address of the object in memory  Primitive types primitive types, written in lower case, have no method  boolean (true|false)<jupyter_code>var result = true;
var anotherResult = false;<jupyter_output><empty_output><jupyter_text>char (character)<jupyter_code>var firstLetter = 'j';<jupyter_output><empty_output><jupyter_text>int (signed 32 bits integer)<jupyter_code>var numberOfLegs = 2;<jupyter_output><empty_output><jupyter_text>double (64 bits floating point)<jupyter_code>var cost = 3.78;<jupyter_output><empty_output><jupyter_text>long and floatsome more exotic types that requires a suffix (`L` or `f`)long (64 bits integers) and float (32 bits floating point numbers)<jupyter_code>var longValue = 123L;
var floatValue = 123.5f;<jupyter_output><empty_output><jupyter_text>byte and shortyou also have byte (a signed 8 bits integer) and short (a signed 16 bits short integer)that are only useful to take less memory when defining an object<jupyter_code>record CompactHeader(byte tag, short version) {}<jupyter_output><empty_output><jupyter_text>when used in variables, they are promoted to 32 bits integerin the following code result is a 32 bits integer (so an int)<jupyter_code>short value = 12;
var result = value + value;<jupyter_output><empty_output><jupyter_text>primitive conversionsYou have automatic conversions if there is no loose of precisionunless it's to convert to double or float which you are always allowed.<jupyter_code>int intValue = 13;
long longValue = intValue;<jupyter_output><empty_output><jupyter_text>you can force conversion in the opposite direction using a castsupplementary bits will be shaved (use with reluctance)<jupyter_code>long longValue = 1_000_000_000_000L;
int intValue = (int) longValue;
System.out.println(intValue);<jupyter_output><empty_output><jupyter_text>ObjectsAll other types are objects, there are two special types, String and arraysthat are object but considered as built-in by the compiler  StringString that stores a text (a sequence of characters)<jupyter_code>var text = "hello";<jupyter_output><empty_output><jupyter_text>Strings have a lot of methods, here is some of themlength of a String<jupyter_code>System.out.println("hello".length());<jupyter_output><empty_output><jupyter_text>to upper/lower case<jupyter_code>System.out.println("hello".toUpperCase());
System.out.println("hello".toLowerCase());<jupyter_output><empty_output><jupyter_text>char at an index (starting with index 0)<jupyter_code>System.out.println("hello".charAt(0));<jupyter_output><empty_output><jupyter_text>index of a character<jupyter_code>System.out.println("hello".indexOf('l'));
System.out.println("hello".indexOf('o'));<jupyter_output><empty_output><jupyter_text>primitive to StringThe fastest and easy way to convert a primitive value to a String isto use the string concatenation (see chapter 'string formatting' for more)<jupyter_code>System.out.println("" + 3);
System.out.println("" + 7.6);<jupyter_output><empty_output><jupyter_text>String to primitiveThere are a bunch of static methods in Boolean, Integer or Double(see chapter 'wrapper' for more info)<jupyter_code>System.out.println(Integer.parseInt("3"));
System.out.println(Double.parseDouble("7.6"));<jupyter_output><empty_output><jupyter_text>Arrayan array initialized with zeros (false, 0, 0.0, etc)<jupyter_code>var intArray = new int[2];<jupyter_output><empty_output><jupyter_text>An array initialized with some default valuesBecause a value like `2` or `3` can be an numeric type(an `int`, a `long`, a `short`, etc)you have to specify the type of the array when you create it<jupyter_code>var intArray = new int[] {2, 3 };
var longArray = new long[] { 2, 3 };<jupyter_output><empty_output><jupyter_text>you can use the operator [] to access or change the valueof an array at a specific index<jupyter_code>System.out.println(intArray[0]);
intArray[0] = 42;
System.out.println(intArray[0]);<jupyter_output><empty_output><jupyter_text>trying to access an array out of its bound raised an exception<jupyter_code>intArray[-1] = 42;   // throws IndexOutOfBoundsException<jupyter_output><empty_output><jupyter_text>and a special syntax to get the length of an arrayNotice that there is no parenthesis when calling length,we will see later why.<jupyter_code>var arrayLength = intArray.length;
System.out.println(arrayLength);<jupyter_output><empty_output><jupyter_text>arrays have methods like \toString()` or `equals()` butthey are not implemented correctly, we will see later why<jupyter_code>System.out.println(intArray);
System.out.println(new int[] {42}.equals(new int[] {42}));<jupyter_output><empty_output><jupyter_text>On arrays We don't use array much in Java, we have morepowerful object like List, that we will see later<jupyter_code>var intList = List.of(2, 3);<jupyter_output><empty_output><jupyter_text>Static methodsBecause primitive types and arrays have (almost) no method,if you want to play with them you have to use static methods.A static method is a function that is declared on a type somewherethat you can call using the syntax `SomeWhere.methodName(arg0, arg1, arg2)` by example to transform a String to an int, we call the methodparseInt stored in the type `java.lang.Integer`<jupyter_code>var resultAsInt = java.lang.Integer.parseInt("42");
System.out.println(resultAsInt);<jupyter_output><empty_output><jupyter_text>To transform an array to a text, there is the static method toStringon the type `java.util.Arrays`<jupyter_code>var text = java.util.Arrays.toString(intArray);
System.out.println(text);<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09907229126553474, 'fraction_numerical': 0.014003150708909505, 'mean_word_length': 7.7236641221374045, 'pattern_counts': {'":': 0, '<': 101, '<?xml version=': 0, '>': 101, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '25', 'dir': 'jupyter-structured-clean-dedup', 'id': '262218', 'license': 'MIT', 'n_tokens_mistral': 1848, 'n_tokens_neox': 1762, 'n_words': 546, 'path': 'jupyter/chapter01-basic_types.ipynb', 'repo_name': 'jeffmaury/java-guide', 'size': '5713'}