I have read in a few places that the default value of a boolean variable is false so I decided to check for myself. My first attempt is shown in prog58 below:
andrew@UBUNTU:~/Java$ cat prog58.java
public class prog58
{
public static void main (String args[])
{
boolean b1;
System.out.println("b1 is " + b1);
}
}
andrew@UBUNTU:~/Java$ javac prog58.java
prog58.java:6: variable b1 might not have been initialized
System.out.println("b1 is " + b1);
^
1 error
andrew@UBUNTU:~/Java$
My second attempt is shown in prog59 below, which does suggest that the default value of a boolean variable is false:
andrew@UBUNTU:~/Java$ cat prog59.java
public class prog59
{
static boolean b1;
public static void main (String args[])
{
System.out.println("b1 is " + b1);
}
}
andrew@UBUNTU:~/Java$ javac prog59.java
andrew@UBUNTU:~/Java$ java prog59
b1 is false
andrew@UBUNTU:~/Java$
The two programs are slightly different. Once I have worked out why one seems to work and the other does not, I will let you know.
Showing posts with label boolean. Show all posts
Showing posts with label boolean. Show all posts
Wednesday, 31 December 2014
Java boolean Variable Default Value
Labels:
boolean,
default value,
Java,
static
Location:
West Sussex, UK
Friday, 4 July 2014
Use Java to Work Out Your Lottery Numbers
To enter the UK lottery, you need to choose six different numbers between 1 and 49.
This program uses a for loop to create 100 lines of lottery numbers.
It stores each line’s values in an array then uses a boolean and a while loop to ensure that each number is only used once per line.
The individual numbers are chosen using Math.random().
In common with many other programming languages, this generates a
random value between 0 and 1 so it has to be converted to give a value
between 1 and 49:
Solaris > cat prog44.java
public class prog44
{
public static void main (String args[])
{
int x;
for(x=1;x<=100;x++)
{
int[] numbers_used = new int[6];
int y;
for (y=0;y<=5;y++)
{
boolean number_found = false;
while (!number_found)
{
int lottery_number =(int)(Math.random()*49)+1;
number_found = true;
int z;
for (z=0;z<y;z++)
{
if (lottery_number == numbers_used[z])
{
number_found = false;
break;
}
}
if (number_found)
{
numbers_used [y] = lottery_number;
if (lottery_number <10) System.out.print(" ");
System.out.print(lottery_number+" ");
}
}
}
System.out.print("\n");
}
}
}
Solaris > javac prog44.java
Solaris > java prog44
38 15 8 36 40 32
47 31 8 22 25 29
34 24 35 19 42 28
4 8 10 6 40 43
35 8 20 19 34 15
27 2 31 42 43 39
13 5 47 1 40 36
22 5 6 1 42 3
41 9 45 29 4 3
27 11 18 26 43 22
34 41 18 30 1 38
11 44 4 42 16 48
21 4 47 41 40 19
49 30 38 12 31 24
35 24 37 49 38 36
36 2 9 30 32 17
17 37 27 32 3 20
31 5 24 44 20 45
22 15 19 9 20 44
24 35 48 36 4 6
41 8 28 42 14 19
34 5 12 2 43 13
23 21 48 11 9 42
39 18 38 34 9 10
10 40 25 49 23 31
7 11 5 17 37 45
15 20 21 9 6 40
14 6 18 15 22 8
5 27 11 8 22 13
46 47 27 9 20 18
17 7 14 18 11 41
44 7 32 8 27 1
33 15 34 19 45 31
44 34 2 7 24 15
49 32 6 41 20 16
38 34 48 25 39 17
4 33 46 43 49 28
26 5 22 46 1 16
30 12 25 19 40 2
49 25 3 31 39 18
48 30 1 13 39 16
25 14 24 37 18 19
3 1 7 43 25 29
46 10 13 22 40 16
8 1 39 21 48 23
37 2 39 14 20 23
11 44 15 21 12 17
27 4 40 3 1 10
36 47 34 13 11 19
43 31 13 29 45 42
1 27 5 33 35 26
1 36 45 49 48 27
22 16 43 17 10 32
44 35 17 15 2 40
1 42 45 14 12 13
29 7 31 16 48 17
3 4 13 42 5 23
20 7 17 28 45 47
22 6 49 48 15 18
12 24 20 44 28 38
3 11 14 25 44 17
10 12 46 5 22 11
45 5 41 39 47 14
49 46 24 43 1 15
40 30 45 35 29 25
18 30 12 38 28 20
7 2 45 49 15 22
25 45 42 6 37 43
43 7 29 19 44 12
4 24 40 41 20 35
17 11 25 31 40 19
9 48 24 33 43 17
43 25 27 37 13 18
5 9 34 12 29 15
23 48 26 31 36 27
15 4 11 29 18 26
49 24 5 44 30 27
15 40 20 37 46 27
17 12 33 10 31 5
36 17 23 4 44 16
29 38 9 4 26 47
17 47 4 19 14 27
11 31 4 25 23 44
27 45 26 6 20 7
29 42 40 37 1 22
38 12 42 3 22 20
40 15 23 17 18 22
27 22 8 29 48 34
43 9 34 46 30 6
26 35 24 5 3 16
4 19 49 28 25 39
21 40 9 37 22 32
8 42 35 12 43 37
38 21 44 18 46 48
1 40 41 48 3 13
19 48 1 27 18 33
26 31 43 19 46 25
40 2 47 26 21 49
20 46 10 8 34 24
22 15 18 33 39 41
Solaris >
Labels:
boolean,
for loop,
Java,
lottery,
Math.random(),
while loop
Location:
West Sussex, UK
Sunday, 5 May 2013
Java boolean Variables
These store the values true or false. There are two in the example below.
The first is called one_greater_than_two and stores the value obtained by evaluating the condition 1 > 2, which is clearly false.
The second is called two_greater_than_one and stores the value obtained by evaluating the condition 2 > 1, which is clearly true.
UBUNTU > cat prog20.java
public class prog20
{
public static void main (String args[])
{
boolean one_greater_than_two = (1 > 2);
boolean two_greater_than_one = (2 > 1);
if (one_greater_than_two)
{System.out.println ("1 > 2");}
if (two_greater_than_one)
{System.out.println ("2 > 1");}
}
}
UBUNTU > javac prog20.java
UBUNTU > java prog20
2 > 1
UBUNTU >
public class prog20
{
public static void main (String args[])
{
boolean one_greater_than_two = (1 > 2);
boolean two_greater_than_one = (2 > 1);
if (one_greater_than_two)
{System.out.println ("1 > 2");}
if (two_greater_than_one)
{System.out.println ("2 > 1");}
}
}
UBUNTU > javac prog20.java
UBUNTU > java prog20
2 > 1
UBUNTU >
If you have a
Java book on Amazon, which you would like to advertise here for free,
please write to me at international_dba@yahoo.co.uk.
Labels:
boolean,
Brian Goetz,
cat,
Java,
javac,
System.out.println,
UBUNTU
Location:
West Sussex, UK
Subscribe to:
Posts (Atom)