0

I have a question regarding these two blocks of code: The first one:

class Solution {
public  int[] twoSum(int[] nums, int target) {
 int[] result= new int[2];
 for (int i=0; i<nums.length; i++){
    for (int j=i+1; j<nums.length; j++){
         if ( nums[i]+nums[j]==target){
            result[0]=i;
            result[1]=j;
         }
     }
 }
 return(result);
 }
}

and the second one:

import java. util.*;

public class Solution {

public static int firstUniqChar(String s) {
    HashMap<String, Integer> map = new HashMap<String, Integer>();
    
    String[] s_arr = s.split("");

    for (int i = 0; i < s_arr.length; i++) {
        if (map.containsKey(s_arr[i])) {
        
            map.put(s_arr[i], map.get(s_arr[i]) + 1);
        }
        else
            map.put(s_arr[i], 1);
    }
    
    for (int i = 0; i < s_arr.length; i++) {
        if (map.get(s_arr[i]) == 1) {
            return i;
        
        }
    }
    return -1;
}

public static void main(String[] args) {
    
    Scanner sc = new Scanner(System.in);
    System.out.println(firstUniqChar(sc.nextLine()));

}   

}

My question is why and how the first code is working without "main"!! And if I want to put "main" for the first code how should I do that? I follow the pattern of the second code regarding static and main and.. but it does not work. Thanks

Azi
  • 15
  • 5
  • It's not? It doesn't. Your assertions of the first one "working" are specious. – Elliott Frisch Mar 09 '23 at 23:19
  • I am checking both in leetcode, and both are working but my questions are 1- How and Why the first one is working without main()?! According to my knowledge, we cannot execute without a main method because when we are running the java program. 2- How I can use main() for the first one? – Azi Mar 09 '23 at 23:26
  • 1
    1- you can't. they must have a "tester" program (with a `main()`) that loads the class and executes it (with unseen code). 2- By writing one and calling `new Solution().twoSum()` with an array and a target value. Best of luck! – Elliott Frisch Mar 09 '23 at 23:35

1 Answers1

0

Add this to the first definition of Solution and run it:

public static void main(String[] args) {
    
    Solution sol = new Solution();
    System.out.println(sol.twoSum(new int[]{1,2,3}, 4));

}   

For any Java program to run it requires a main method with signature as follows. It’s the entry-point or start point. If it’s not in your code it is provided by the surrounding code - online or IDE.

public static void main(String[] args)

Any static method (firstUniqChar) can be called without referring to an object. A non-static method (twoSum) is linked to an object so has to be invoked from an object, in this case sol

John Williams
  • 4,252
  • 2
  • 9
  • 18
  • I wrote: public static void main(String[] args) { Solutiontest1 sol = new Solutiontest1(); System.out.println(sol.twoSum({1,2,3}, 4)); } and recived : Exception in thread "main" java.lang.Error: Unresolved compilation problems: Syntax error on token ".", @ expected after this token Syntax error, insert ")" to complete SingleMemberAnnotation Syntax error, insert "SimpleName" to complete ArgumentList Syntax error on token ")", delete this token at java_test1/java_test1.Solutiontest1.main(Solutiontest1.java:20) – Azi Mar 10 '23 at 00:58
  • I left out new on new int[]. Fixes the answer. – John Williams Mar 10 '23 at 08:27