Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Null issue needs an explanation

Tags:

java

I am a newbie at programming and Java and this is my first null, I am a little bit confused because I do not know what happened is that kind of errors in coding? Or any thing else? Kindly needs your explanation in this situation and about null at overall in a simple way

public static void main(String[] args) {
    Scanner input = new Scanner (System.in);
    System.out.println("Enter grades size :");

    int Size = input.nextInt();

    String[] y = new String[Size];
    int[] x = new int[Size];

    int Max = 0;
    int Min = x[0];

    String Max_studen = y[0];
    String Min_studen = y[0];

    for (int i = 0 ; i < Size ; i++) {
        System.out.println("Enter student name #" + (i));
        y[i] = input.next();

        System.out.println("Enter student grade : #" + (i));
        x[i] = input.nextInt();

        if (x[i] > Max) {
            Max = x[i];
            Max_studen = y[i];
        } else if (x[i] < Min) {
            Min = x[i];
            Min_studen = y[i];
        }
    }

    System.out.println("Max Student is: " + Max_studen);
    System.out.println("Max grade is: ");
    System.out.println(Max);
    System.out.println("Min Student is: " + Min_studen);
    System.out.println("Min grade is : ");
    System.out.println(Min);
}

Output is:

Enter grades size :
2
Enter student name #0
Sam
Enter student grade : #0
85
Enter student name #1
Samy
Enter student grade : #1
95
Max Student is: Samy
Max grade is:
95
Min Student is: null
Min grade is :
0 
like image 374
mostafa magdy Avatar asked Jul 22 '15 02:07

mostafa magdy


People also ask

Why is null necessary?

Null can be useful because it is a guaranteed value that indicates that something is wrong, or outside of the domain/range of possible answers.

What is null in Java?

Null keyword Null is a reserved keyword in the Java programming language. It's technically an object literal similar to True or False. Null is case sensitive, like any other keyword in Java.

What is the error of the default null hypothesis?

The Error of the Default or Irrefutable Null. There are two forms of such tampering with the null hypothesis: /philosophy : pseudoscience : incorrect hypothesis testing/ : a variation of argument from ignorance. The practice of assigning a favored or untestable/unfalsifiable hypothesis unmerited status as the null hypothesis.

Why is the null hypothesis useful in research?

The null hypothesis is useful because it can be tested and found to be false, which then implies that there is a relationship between the observed data. It may be easier to think of it as a nullifiable hypothesis or one that the researcher seeks to nullify. The null hypothesis is also known as the H 0, or no-difference hypothesis.

What is a null subject?

Dr. Richard Nordquist is professor emeritus of rhetoric and English at Georgia Southern University and the author of several university-level grammar and composition textbooks. A null subject is the absence (or apparent absence) of a subject in a sentence.

What is the difference between null and significance test?

To distinguish it from other forms of a hypothesis, the null hypothesis is written ​H0 (which is read as “H-nought”, "H-null", or "H-zero"). A significance test is used to determine the likelihood the results supporting the null hypothesis are not due to chance.


1 Answers

For a beginner, this is a tricky one. Let us walk through this code together.

The first important lines are those:

String[] y = new String[Size];
int[] x = new int[Size];

There is a major difference between those two arrays. x is an array of primitives, while y is an array of objects. Primitives have a well-defined default values in Java (typically 0), so the array x is initialized with all cells containing a 0. For objects there is no such thing as a default value. Since you can write your own class, Java does not know what a good default value for, e.g., a Car is. Therefore the default value for objects is null. For the moment, let us define null as a value signaling "This cell is empty" 1. So each cell of y contains null as its value.

Now to the next important lines:

int Min = x[0];
[...]
String Min_studen = y[0];

As explained earlier, x is initialized with all 0. Concluding from this, Min is now 0. We have a problem here because in a typical scenario we will never enter a grade < 0 and therefore Min will never be overwritten. In genereal, if we are looking for some maximum or minimum, we should initialize them with Integer.MIN_VALUE (for the maximum) or Integer.MAX_VALUE (for the minimum) respectively. There are similar constants for long, float and double. Theses constants typically avoid problems like the given one. Since Min is never overwritten, Min_studen is never overwritten either. The initial values of y are nulls, as is Min_studen's value.


Some remarks on your code:

  • Variable names should be written in camelCase.
  • The [] of an array is typically written following the type, without a blank: String[] instead of String [].
  • After a {, there should always be a linebreak.
  • Variable names should be meaningful and descriptive. One could use studentGrades and studentNames instead of x and y.

There are many sites explaining coding conventions and style guides, some of them are here and here 2. You might not want to look at them right now, but keep them in mind for later, if you got the basics of Java down and read them.


1The actual meaning of null is quite close to the description I gave, but the reasoning is a little bit more complex and requires the reader to have knowledge about heap- and stackmemory and what is stored where.

2There is no such thing as "the styleguid". You are of course free to define your own. Style guides are a means to make code easier to read. Please keep this in mind whenever you change your style guide or parts of it.

like image 160
Turing85 Avatar answered Nov 15 '22 10:11

Turing85