Ask Programiz - Human Support for Coding Beginners

Explore questions from fellow beginners answered by our human experts. Have some doubts about coding fundamentals? Enroll in our course and get personalized help right within your lessons.

  • All
  • Python
  • C
  • Java
  • CPP
  • SQL
  • JS
  • HTML
  • CSS
Udayan Shakya
Expert
last week
Udayan Shakya answered

Hello Rocky! I see you're taking your Java lessons earnestly, and it's really encouraging to see learners like you!

And your hunch about type declaration is right on the mark:

Every time you declare a new variable, you need to specify its type. You don't need to specify type for the variable after you've declared it.

Let's clarify this with a modified version of the program you've given:

class Main {
    public static void main(String[] args) {
     
        // Create and print the distance variable
        int distance = 134;
        System.out.println("Original distance = " + distance);
     
        // Create another variable distance2
        int distance2 = 564;
        System.out.println("Original distance2 = " + distance2);
     
        // Assign another value to distance
        // No need to declare type again
        distance = 760;
        System.out.println("\nModified distance = " + distance);
     
        // Assign another value to distance2
        // No need to declare type again
        distance2 = 666;
        System.out.println("Modified distance2 = " + distance2);
     }
}

Output

Original distance = 134
Original distance2 = 564

Modified distance = 760
Modified distance2 = 666

Notice the following blocks of code above:

distance = 760;
System.out.println("\nModified distance = " + distance);
     
distance2 = 666;
System.out.println("Modified distance2 = " + distance2);

You can clearly see that you don't need to redeclare the types of distance and distance2 because they've already been declared when you created the variables:

// Declaration of 'distance' variable
int distance = 134;


// Declaration of 'distance2' variable
int distance2 = 564;

This is true not just for Java, but for other programming languages like C, C++, TypeScript, etc. as well.

Hope that clears things up! Contact us again if you have further questions!

Java
This question was asked as part of the Learn Java Basics course.
Anuj Shrestha
Expert
2 weeks ago
Anuj Shrestha answered

Hi Az! That’s a great question, and I really like your mindset of trying to understand why things behave the way they do. This approach is very important when learning programming, keep it up.

To answer your question, in Java, the data types of the operands used in an operation determine how the calculation is performed.

In this code:

class Main {
    public static void main(String[] args) {

        int number = 12;
        int result = number / 8;
        System.out.println(result);
    }
}

Both number and 8 are of type int. Because of this, Java performs integer division.

Integer division removes the decimal part, so 12 / 8 is evaluated as 1, which is why the output is 1.

Even if you store the result in a double, the calculation still happens using integers:

int number = 12;
double result = number / 8;
System.out.println(result); // prints 1.0

Here, the division produces 1 first, and then Java converts it to 1.0 when assigning it to the double.

To get a decimal result like 1.5, at least one operand must be a double before the division and data type of variable used as result should be double too:

class Main {
    public static void main(String[] args) {

        int number = 12;
        double result = number / 8.0;
        System.out.println(result); // 1.5
    }
}

Here, 8.0 is a double, so Java uses floating-point division and preserves the decimal value.

Hope that helps. Feel free to reply here or in any other topics if you have doubts.

Java
This question was asked as part of the Learn Java Basics course.
Abhay Jajodia
Expert
2 weeks ago
Abhay Jajodia answered

The next() and nextLine() methods are both part of the Scanner class in Java and are used for reading input from users, but they serve slightly different purposes:

  • next(): It reads the next token (word) from the input, stopping at whitespace (spaces, tabs, or newlines).

    For example, if the user inputs Maria Smith, calling next() would only return Maria on the first call and Smith on the second call.

  • nextLine(): It reads the entire line of input until it hits a newline character (when the user presses Enter).

    So in the case of the same input Maria Smith, calling nextLine() would return Maria Smith all at once.

As highlighted in your lesson, it's recommended to use nextLine() when you want to capture full inputs that may consist of multiple words, like names or sentences, without worrying about spaces.

This is particularly useful in scenarios where the input contains spaces, and you want to ensure you capture everything as a single string.

If you have any more questions about these methods or anything else, feel free to ask! Hope this helps!

Java
This question was asked as part of the Learn Java Basics course.
K
PRO
last month
Kartikcountry asked
Udayan Shakya
Expert
last month
Udayan Shakya answered

Hello, Kartik! You take user input by creating an object of the Scanner class, and then using the object to call the appropriate method, such as nextInt() for integers, nextDouble() for floating-point numbers, and nextLine() for strings.

For example,

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        
        System.out.println("Enter your age: ");
        
        // Create a Scanner object
        Scanner input = new Scanner(System.in);
        
        // Take user input 
        int age = input.nextInt();
        
        System.out.println("Age is " + age);

    }
}

The basics of user input has already been explained in the Taking Input lesson of Chapter 1: Introduction.

Please review that lesson again before proceeding further.

Hope that helps! Contact us again if you have further questions.

Java
This question was asked as part of the Learn Java Basics course.
Abhay Jajodia
Expert
last month
Abhay Jajodia answered

Hello Pratik, really nice question.

charAt() is a method in Java that lets you grab a single character from a string. You just give it the position you want, and it hands you back the character at that spot.

Java starts counting from zero, so:

  • the first character is at index 0

  • the second is at index 1

  • and so on

For example:

String text = "Hello";
char c = text.charAt(1); // 'e'

If you try an index that doesn’t exist, Java throws an error because the string doesn't go that far.

You’ll use charAt() a lot when you loop through a string and check each character, like when counting vowels.

If you have further questions, I'm here to help.

Java
This question was asked as part of the Java Interview Questions course.
Kelish Rai
Expert
last month
Kelish Rai answered

Hi quiet,

You're absolutely right .

In Java, Math.random() returns a value from 0.0 (inclusive) to 1.0 (exclusive). So:

Math.random() * 10    // gives a value from 0.0 to less than 10.0  
Math.random() * 10 + 1  // gives a value from 1.0 to less than 11.0

So the correct range for Math.random() * 10 + 1 is 1.0 (inclusive) to less than 11.0 (exclusive).

If you have more questions, I am here to help.

Java
This question was asked as part of the Learn Java Basics course.
Abhay Jajodia
Expert
last month
Abhay Jajodia answered

Hi Miki,

Great question — this is a common source of confusion in Java.

  • == checks if two variables point to the exact same object in memory. It doesn’t care about the content — just the reference.

  • .equals() checks if the content of two strings is the same, even if they’re different objects.

Example:

String a = new String("hello");
String b = new String("hello");

System.out.println(a == b);       // false — different objects
System.out.println(a.equals(b));  // true — same content

So whenever you want to compare the actual text in two strings, always use .equals().

If you have more questions, I am here to help.

Java
This question was asked as part of the Practice: Java Basics course.
Abhay Jajodia
Expert
last month
Abhay Jajodia answered

Hi Yeah, great question.

In Java, input.nextLine() is used to read an entire line of text entered by the user — including spaces — up until they press ENTER. This makes it ideal when you're asking for inputs like full names, addresses, or any sentence.

Here's how it works:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.println("Enter your full name:");
        String fullName = input.nextLine();

        System.out.println("Hello, " + fullName);
    }
}

If the user types John Doe, the output will be:

Hello, John Doe

Now, how is it different from nextInt() or next()?

  • nextInt() only reads the integer.

  • next() reads a single word (stops at a space).

  • nextLine() reads the entire line, including any spaces, until ENTER is pressed.

This also means that if you use nextInt() followed by nextLine(), you may get unexpected behavior because nextInt() doesn't consume the newline character. That’s something to watch out for.

If you have more questions, I am here to help.

Java
This question was asked as part of the Learn Java Basics course.
Youssef Beshara
3 months ago
Youssefcountry asked
Sarthak Baral
Expert
3 months ago
Sarthak Baral answered

Hey there! Great question, and it's a common one when starting with Java!

Yes, Java does indeed have a type called double. Here's a straightforward explanation:

Types of Numbers in Java:

  1. Integers:
  2. Whole numbers without decimals (e.g., 5, -11, 0).

  3. Floating-Point Numbers:

  4. Allow representation of numbers with decimals.
  5. There are two types in Java:
    • Float: Uses 4 bytes (single precision)
    • Double: Uses 8 bytes (double precision)

So, what is a double? - double is a data type used for floating-point numbers with double precision. - It can handle larger numbers and provide more precision than float. - Here's how you might use it:

public class Main {
    public static void main(String[] args) {
        double myDouble = 3.14159;
        System.out.println(myDouble);  // This will print: 3.14159
    }
}

In your lesson, when you refer to floating-point numbers, it's often using the double type due to its precision and flexibility.

Feel free to try running the example above, and let me know if you have any questions or need more clarity! Happy coding! 🌟

Java
This question was asked as part of the Learn Java Basics course.
Vunnam Sowmya
3 months ago
Vunnamcountry asked
Abhay Jajodia
Expert
last month
Abhay Jajodia answered

Hi Vunnam Sowmya,

Case-sensitive means uppercase and lowercase letters are treated as different.

So "Java" and "java" are not the same, because J and j are different characters.

If you have more questions, I am here to help 😊

Java
This question was asked as part of the Learn Java Basics course.