Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By jim829

Thread: Basic questions to kick off with Java

  1. #1
    czezz is offline Member
    Join Date
    Feb 2017
    Posts
    1
    Rep Power
    0

    Default Basic questions to kick off with Java

    Hi All :)
    This is my first post on this forum.
    I am new to Java and I have some basic questions that hopefully help me to start up.

    I have committed my first code (actually it is somebody's code that I have modified). I have compiled it using NebeansIDE (java version "1.8.0_45").
    Following code is to move mouse cursor automatically.

    Java Code:
    package mmover;
    import java.awt.Robot;
    import java.util.Random;
    import java.sql.Timestamp;
    import java.util.Date;
    
    public class Mmover {
    
        public static final int FIVE_SECONDS = 5000;
        public static final int MAX_Y = 400;
        public static final int MAX_X = 400;
        
        public static void main(String... args) throws Exception {
            Robot robot = new Robot();
            Random random = new Random();
    
                          
            while (true) {
                robot.mouseMove(random.nextInt(MAX_X), random.nextInt(MAX_Y));
               //Thread.sleep(FIVE_SECONDS);
               Thread.sleep(random.nextInt(FIVE_SECONDS)); 
                
               //Date object
    	   Date date = new Date();
               //getTime() returns current time in milliseconds
    	   long time = date.getTime();
               //Passed the milliseconds to constructor of Timestamp class 
    	   Timestamp ts = new Timestamp(time);
    	                         
               System.out.println("Interval: " + random.nextInt(FIVE_SECONDS) + " sec, Executed at: " + ts + " position X: " + random.nextInt(MAX_X) + " Y: " + random.nextInt(MAX_Y));
            }
        }
    }
    My questions:
    1. Each java code is started with: import {something}. I guess this {something} is some sort of library? From where do I take list of these libraries?
    2. From where did the initial author of this code knew that he needs tu use: ava.awt.Robot and java.util.Random?
    3. In this code I have added Timestamp (searched in google). I had to put it all under while loop to get current/fresh value after each look execution.
    Is it possible in Java/How can I define Timestamp variable in one place in the code (eg. at the top of the code) and then later print it out in different parts of the code (like eg. in the while loop) with current/fresh value?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Basic questions to kick off with Java

    1. The import statement provides a path to the classes' definitions so the compiler can find them.
    2. The classes' functions are described in: Java Platform SE 8

    For more information read the tutorial: The Really Big Index
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,342
    Rep Power
    12

    Default Re: Basic questions to kick off with Java

    There are different ways to deal with time, you should definitely know learn about each of these:

    Java Code:
        System.out.println(new Date());
        System.out.println(new SimpleDateFormat("YYYY/mm/dd hh:mm:ss").format(new Date()));
        System.out.println(System.currentTimeMillis());
        System.out.println(System.nanoTime());
        System.out.println(new Timestamp(System.currentTimeMillis()));
    All of these can be found in the official API docs that Norm linked above. Notice the third one - you can get the current time in milliseconds any time without the need of a date object. Cool! You can also reuse your TimeStamp if you want.

    Declare it outside your loop, and then look at the API docs for it - it has a setTime() method. You can use this with the millisecond method above to set the time on each loop iteration. Consider this:

    Java Code:
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        for(int i=0; i<10; i++){
            System.out.println(timestamp);
            timestamp.setTime(System.currentTimeMillis());
            Thread.sleep(1000);
        }

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Basic questions to kick off with Java

    A couple of things. I would be careful of the Date class ince quite a few of the methods are deprecated. And Calendar is old too. As of Java 8 I would check out the LocalTime class for doing time related things.

    Regards,
    Jim
    quad64bit likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,342
    Rep Power
    12

    Default Re: Basic questions to kick off with Java

    Thanks for the tip, my java is getting dated! Har Har . It would be useful for the novice to note that the LocalTime class does not take timezones into account unless you specify an offset, which is a little different from some of the older Date/time representations one might see around.

  6. #6
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,993
    Rep Power
    9

    Default Re: Basic questions to kick off with Java

    Quote Originally Posted by quad64bit View Post
    Thanks for the tip, my java is getting dated! Har Har . It would be useful for the novice to note that the LocalTime class does not take timezones into account unless you specify an offset, which is a little different from some of the older Date/time representations one might see around.
    Well, you have been absent for the past 2 years. Welcome back :)
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Similar Threads

  1. Some basic questions - Java
    By Fusion in forum New To Java
    Replies: 4
    Last Post: 04-02-2014, 12:54 AM
  2. Need to kick start with java!
    By saravanataee in forum Introductions
    Replies: 2
    Last Post: 05-11-2012, 03:33 PM
  3. Replies: 3
    Last Post: 04-17-2012, 07:15 PM
  4. VERY basic Java questions
    By hayden06f4i in forum New To Java
    Replies: 6
    Last Post: 11-10-2010, 09:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •