Java Basics: Numbers, Characters, and String Handling – Day 53 Learning Notes
This tutorial walks through Java's core numeric and character utilities, covering number wrapper classes and autoboxing/unboxing, the Number hierarchy, formatted output with printf/format, DecimalFormat patterns, and the Math class’s constants and methods for arithmetic, logarithmic, trigonometric, and random number generation, all illustrated with concrete code examples.
The article introduces Java's java.lang core package and explains how basic data types can be wrapped into objects using the wrapper classes introduced in JDK 5.0. It describes automatic boxing and unboxing, showing a simple example where Integer a = 45; and Integer b = 34; are automatically boxed, then unboxed for the addition in System.out.println(a + b);.
It then lists the advantages of using Number objects over primitive types: they can be passed as method parameters, provide constants such as minValue and maxValue, and offer conversion utilities between numeric types and strings. A hierarchy diagram of Number subclasses is shown in an image.
Next, the article covers formatted output. It replaces the usual System.out.print and println calls with printf and format, presenting the method signatures:
public PrintStream printf(String format, Object... args);
public PrintStream format(String format, Object... args);An example prints a double value using format specifiers:
int num = 4421;
System.out.format("%d%n", num);The article also provides a table of common format specifiers and flags.
For more precise control, the java.text.DecimalFormat class is introduced. Sample patterns such as "#,#00.###" and "¥##,###,###" are applied to numbers, with the resulting formatted strings displayed in the output section.
The Math class is then explored. It lists the constants Math.E and Math.PI, and demonstrates static methods for basic arithmetic, exponentiation, logarithms, and power functions. Example code prints the value of e, computes exp(9.123), log(9.123), pow(9.123, 16.445), and sqrt(9.123).
Trigonometric functions are covered, noting that Java expects angles in radians. The article shows how to convert degrees to radians with Math.toRadians and then prints sine, cosine, tangent, hyperbolic cosine, and their inverse functions, using format to display the results.
Finally, random number generation is discussed. The static Math.random() method returns a double in the range [0,1). To obtain an integer between 0 and 9, the article multiplies the result by 10 and casts to int. It also mentions the java.util.Random class for generating sequences of random numbers.
Throughout, code snippets are presented inside ... tags, and illustrative images of class hierarchies and format tables are retained.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Lisa Notes
Lisa's notes: musings on daily life, work, study, personal growth, and casual reflections.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
