From 2bf62eb9563778245671a76cae75eaf432da5c3f Mon Sep 17 00:00:00 2001
From: Tristan-Raz
+ With that distinction in mind, here are the common types you'll use, most of which are similar to Python's types: +
+ int: The primitive type for integers (whole numbers), such as 3, 0, and -76. +
++ double: The primitive type for floating-point numbers like 6.3 or -0.9. +
++ boolean: The primitive type that can only be true or false. +
++ char: The primitive type for a single character, like 'a' or 'Z'. It is represented using single quotes. +
++ String: An object type that represents a sequence of characters in double quotes, like "Hello". +
+Let’s look at a simple Python function which converts a Fahrenheit temperature to Celsius. @@ -117,11 +150,6 @@ public class TempConv {
-- Input/Output and the Scanner Class -
-
- Here is where we run into one of the most important differences between Java and Python. Python is a
+ A valid variable name in Java can contain letters, digits, and underscores. It must begin with a letter, an underscore, or a dollar sign. It cannot start with a digit and it cannot be a reserved keyword (like class, int, or static). Variable names are case-sensitive, so
+ An important feature of Java is that when you declare a variable of a primitive type (like int or double), the system automatically allocates a fixed amount of memory to store its value directly. This is different from reference types (like String or Scanner), where the variable holds a memory address that points to the actual object data stored elsewhere. This distinction makes operations on primitives very fast. +
In the example above, lines 5—7 contain variable declarations. Specifically we are saying that
- The general rule in Java is that you must decide what kind of an object your variable is going to reference and then you must declare that variable before you use it. There is much more to say about the static typing of Java, but for now this is enough. + The general rule in Java is that you must decide what kind of an object your variable is going to reference and then you must declare that variable before you use it. In our temperature converter, the calculation (fahr - 32) * 5.0/9.0 works correctly because 5.0 and 9.0 are treated as double values, preventing the integer division that would occur if we had written 5/9, which would result in 0.
-
- In the previous section we created a
- On line 11 we use the
- The table below shows some commonly used methods of the
+ In this example, we first create a