Java Math Method
Java Math Method
1. Mathematical Functions
Java Math class provides several methods to work on math calculations like min(), max(), avg(),
sin(), cos(), tan(), round(), ceil(), floor(), abs() etc.
1. public class JavaMathExample1
2. {
3. public static void main(String[] args)
4. {
5. double x = 28;
6. double y = 4;
7.
8. // return the maximum of two numbers
9. System.out.println("Maximum number of x and y is: " +Math.max(x, y));
10.
11. // return the square root of y
12. System.out.println("Square root of y is: " + Math.sqrt(y));
13.
14. //returns 28 power of 4 i.e. 28*28*28*28
15. System.out.println("Power of x and y is: " + Math.pow(x, y));
16.
17. // return the logarithm of given value
18. System.out.println("Logarithm of x is: " + Math.log(x));
19. System.out.println("Logarithm of y is: " + Math.log(y));
20.
21. // return the logarithm of given value when base is 10
22. System.out.println("log10 of x is: " + Math.log10(x));
23. System.out.println("log10 of y is: " + Math.log10(y));
24.
25. // return the log of x + 1
26. System.out.println("log1p of x is: " +Math.log1p(x));
27.
28. // return a power of 2
29. System.out.println("exp of a is: " +Math.exp(x));
30.
31. // return (a power of 2)-1
32. System.out.println("expm1 of a is: " +Math.expm1(x));
33. }
34. }
Integrative Programming Examples about Mathematical Functions, Characters, and Strings
Output:
Output:
------------------------------****************************----------------------------
---------------********************-----------------****************------------
import java.math.*;
public class NewClass
{
}
}
Output:
Initial value of int : -1
Initial value of int : 0.5
Absolute value of int : 1
Absolute value of int : 0.5
acos value of Acosi : NaN
acos value of Acosj : 1.5159376794536454
➢ What is \u0000?
--------------------**********************------------------------------
Example 1
Let's see a simple example of displaying characters.
Output:
char1: a
char2: A
Integrative Programming Examples about Mathematical Functions, Characters, and Strings
Example 2
//You don’t have to know the Unicode of each character; these are just as examples to show
you how Java could deal with these Unicode
In this example, we provide integer value to char variable. Here, compiler implicitly
typecast integer to char and display the corresponding ASCII value.
Output:
char1: A
char2: a
Example 3
In this example, we typecast the integer value to char explicitly.
Output:
char1: a
char2: A
Example 4
In this example, we increment the provided char value by 1.
Output:
char: B
Example 5
//We can understand this example after we know how array works in Java
1. import java.util.Arrays;
2.
3. public class CharExample6 {
4.
Integrative Programming Examples about Mathematical Functions, Characters, and Strings
Output:
String: javatpoint
char: [j, a, v, a, t, p, o, i, n, t]
3. String in Java
// create a string
String type = "Java programming";
Here, we have created a string variable named type. The variable is initialized with
the string Java Programming.
Note: Strings in Java are not primitive types (like int , char , etc). Instead, all strings
are objects of a predefined class named String .
And, all string variables are objects of the String class.
Integrative Programming Examples about Mathematical Functions, Characters, and Strings
class Main {
public static void main(String[] args) {
// create strings
String first = "Java";
String second = "Python";
String third = "JavaScript";
// print strings
System.out.println(first); // print Java
System.out.println(second); // print Python
System.out.println(third); // print JavaScript
}
}
In the above example, we have created three strings named first, second, and third.
Here, we are directly creating strings like primitive types.
However, there is another way of creating Java strings (using the new keyword).
Java String Operations
Java String provides various methods to perform different operations on strings. We
will look into some of the commonly used string operations.
To find the length of a string, we use the length() method of the String. For example,
class Main {
public static void main(String[] args) {
// create a string
String greet = "Hello! World";
System.out.println("String: " + greet);
// get the length of greet
int length = greet.length();
System.out.println("Length: " + length);
}
}
Integrative Programming Examples about Mathematical Functions, Characters, and Strings
Output
In the above example, the length() method calculates the total number of characters
in the string and returns it.
We can join two strings in Java using the concat() method. For example,
class Main {
public static void main(String[] args) {
// create second
String second = "Programming";
System.out.println("Second String: " + second);
Output
In the above example, we have created two strings named first and second . Notice the
statement,
Here, we the concat() method joins first and second and assigns it to the joinedString variable.
In Java, we can make comparisons between two strings using the equals() method.
For example,
class Main {
public static void main(String[] args) {
// create 3 strings
String first = "java programming";
String second = "java programming";
String third = "python programming";
Output
In the above example, we have created 3 strings named first, second, and third. Here, we
are using the equal() method to check if one string is equal to another.
The equals() method checks the content of strings while comparing them.
Note: We can also compare two strings using the == operator in Java. However, this
approach is different than the equals() method.
Integrative Programming Examples about Mathematical Functions, Characters, and Strings
https://www.youtube.com/watch?v=iTC43mLZG38&t=176s
Since strings are represented by double quotes, the compiler will treat "This is the
" as the string. Hence, the above code will cause an error.
To solve this issue, we use the escape character \ in Java. For example,
Now escape characters tell the compiler to escape double quotes and read the whole
text.
Java Strings are Immutable
In Java, strings are immutable. This means, once we create a string, we cannot change
that string.
To understand it more deeply, consider an example:
// create a string
String example = "Hello! ";
Here, we have created a string variable named example . The variable holds the
string "Hello! " .
Here, we are using the concat() method to add another string World to the previous string.
It looks like we are able to change the value of the previous string. However, this is
not true .
Let's see what has happened here,
Since strings in Java are objects, we can create strings using the new keyword as well.
For example,
In the above example, we have created a string name using the new keyword.
Here, when we create a string object, the String() constructor is invoked.
class Main {
public static void main(String[] args) {
If we create the string using new keyword, we will not be able to use = = to
compare between two strings
class Main {
public static void main(String[] args) {
if (a == b)
System.out.println("True");
else
System.out.println("False");
}
}
The output will be False, even though both strings equal "car".