जावा अभिकथन (अभिकथन कथन)

इस ट्यूटोरियल में, हम उदाहरणों की मदद से Java मुखर कथन (Java अभिकथन) के बारे में जानेंगे।

जावा में अभिक्रियाएँ बग को पहचानने में मदद करती हैं कि हम परीक्षण कोड को सत्य मानते हैं।

assertकीवर्ड का उपयोग करके एक जोर दिया जाता है ।

इसका सिंटैक्स है:

 assert condition;

यहाँ, conditionएक बूलियन अभिव्यक्ति है जिसे हम प्रोग्राम के निष्पादित होने पर सच मान लेते हैं।

समर्थकों को सक्षम बनाना

डिफ़ॉल्ट रूप से, दावे को अक्षम किया जाता है और रनटाइम पर ध्यान नहीं दिया जाता है।

दावे सक्षम करने के लिए, हम उपयोग करते हैं:

 java -ea:arguments

या

 java -enableassertions:arguments

जब दावे सक्षम होते हैं और शर्त होती है true, तो कार्यक्रम सामान्य रूप से निष्पादित होता है।

लेकिन अगर शर्त का मूल्यांकन करने के लिए स्थिति का मूल्यांकन किया जाता है false, तो JVM एक फेंकता है AssertionError, और प्रोग्राम तुरंत बंद हो जाता है।

उदाहरण 1: जावा अभिकथन

 class Main ( public static void main(String args()) ( String() weekends = ("Friday", "Saturday", "Sunday"); assert weekends.length == 2; System.out.println("There are " + weekends.length + " weekends in a week"); ) ) 

आउटपुट

 एक सप्ताह में 3 सप्ताह होते हैं 

हम उपरोक्त आउटपुट प्राप्त करते हैं क्योंकि इस कार्यक्रम में कोई संकलन त्रुटियां नहीं हैं और डिफ़ॉल्ट रूप से, दावे अक्षम हैं।

दावे को सक्षम करने के बाद, हमें निम्नलिखित आउटपुट मिलते हैं:

 थ्रेड में अपवाद "मुख्य" java.lang.AssertionError 

कथन का दूसरा रूप

 assert condition : expression; 

इस कथन के रूप में, एक अभिव्यक्ति AssertionErrorवस्तु के निर्माता को दी जाती है। इस अभिव्यक्ति का एक मान होता है जो स्थिति होने पर त्रुटि के विवरण संदेश के रूप में प्रदर्शित होता है false

विस्तृत संदेश का उपयोग समस्या को डीबग करने में मदद करने के लिए जोर विफलता की सूचना को पकड़ने और संचारित करने के लिए किया जाता है।

उदाहरण 2: एक्सप्रेशन उदाहरण के साथ जावा अभिकथन

 class Main ( public static void main(String args()) ( String() weekends = ("Friday", "Saturday", "Sunday"); assert weekends.length==2 : "There are only 2 weekends in a week"; System.out.println("There are " + weekends.length + " weekends in a week"); ) ) 

आउटपुट

 धागे में अपवाद "मुख्य" java.lang.AssertionError: एक सप्ताह में केवल 2 सप्ताह होते हैं 

जैसा कि हम ऊपर के उदाहरण से देखते हैं, अभिव्यक्ति AssertionErrorऑब्जेक्ट के कंस्ट्रक्टर को पास की जाती है। यदि हमारी धारणा है falseऔर दावे सक्षम हैं, तो एक अपवाद एक उपयुक्त संदेश के साथ फेंक दिया जाता है।

यह संदेश उस त्रुटि का निदान करने और उसे ठीक करने में मदद करता है जिससे दावे विफल हो गए।

विशिष्ट वर्गों और पैकेजों के लिए जोर लगाना

यदि हम अभिकथन कमांड-लाइन स्विच को कोई तर्क नहीं देते हैं,

 जावा -आ 

यह सिस्टम कक्षाओं को छोड़कर सभी वर्गों में अभिकथन देता है।

हम तर्कों का उपयोग करके विशिष्ट वर्गों और पैकेजों के लिए दावे को सक्षम कर सकते हैं। इन कमांड-लाइन स्विच को प्रदान किए जाने वाले तर्क निम्न हैं:

वर्ग नामों में मुखरता सक्षम करें

हमारे कार्यक्रम के सभी वर्गों के लिए मुखरता को सक्षम करने के लिए,

 java -ea Main

केवल एक वर्ग को सक्षम करने के लिए,

 java -ea:AnimalClass Main

यह केवल कार्यक्रम AnimalClassमें जोर देता है Main

पैकेज नामों में मुखरता सक्षम करें

com.animalकेवल पैकेज और इसके उप- पैकेज के लिए अभिकथन सक्षम करने के लिए ,

 java -ea:com.animal… Main

अनाम पैकेज में जोर सक्षम करें

वर्तमान कार्य निर्देशिका में अनाम पैकेज (जब हम पैकेज स्टेटमेंट का उपयोग नहीं करते हैं) में दावे को सक्षम करने के लिए।

 java -ea:… Main

सिस्टम कक्षाओं में अभिकथन सक्षम करें

सिस्टम कक्षाओं में जोर देने के लिए, हम एक अलग कमांड-लाइन स्विच का उपयोग करते हैं:

 java -esa:arguments 

या

 java -enablesystemassertions:arguments

इन स्विचों को प्रदान किए जाने वाले तर्क समान हैं।

दावे को अक्षम करना

दावे को अक्षम करने के लिए, हम उपयोग करते हैं:

 java -da arguments 

या

 java -disableassertions arguments 

To disable assertion in system classes, we use:

 java -dsa:arguments

OR

 java -disablesystemassertions:arguments

The arguments that can be passed while disabling assertions are the same as while enabling them.

Advantages of Assertion

  1. Quick and efficient for detecting and correcting bugs.
  2. Assertion checks are done only during development and testing. They are automatically removed in the production code at runtime so that it won’t slow the execution of the program.
  3. It helps remove boilerplate code and make code more readable.
  4. Refactors and optimizes code with increased confidence that it functions correctly.

When to use Assertions

1. Unreachable codes

Unreachable codes are codes that do not execute when we try to run the program. Use assertions to make sure unreachable codes are actually unreachable.

Let’s take an example.

 void unreachableCodeMethod() ( System.out.println("Reachable code"); return; // Unreachable code System.out.println("Unreachable code"); assert false; ) 

Let’s take another example of a switch statement without a default case.

 switch (dayOfWeek) ( case "Sunday": System.out.println("It’s Sunday!"); break; case "Monday": System.out.println("It’s Monday!"); break; case "Tuesday": System.out.println("It’s Tuesday!"); break; case "Wednesday": System.out.println("It’s Wednesday!"); break; case "Thursday": System.out.println("It’s Thursday!"); break; case "Friday": System.out.println("It’s Friday!"); break; case "Saturday": System.out.println("It’s Saturday!"); break; ) 

The above switch statement indicates that the days of the week can be only one of the above 7 values. Having no default case means that the programmer believes that one of these cases will always be executed.

However, there might be some cases that have not yet been considered where the assumption is actually false.

This assumption should be checked using an assertion to make sure that the default switch case is not reached.

 default: assert false: dayofWeek + " is invalid day"; 

If dayOfWeek has a value other than the valid days, an AssertionError is thrown.

2. Documenting assumptions

To document their underlying assumptions, many programmers use comments. Let’s take an example.

 if (i % 2 == 0) (… ) else ( // We know (i % 2 == 1)… ) 

Use assertions instead.

Comments can get out-of-date and out-of-sync as the program grows. However, we will be forced to update the assert statements; otherwise, they might fail for valid conditions too.

 if (i % 2 == 0) (… ) else ( assert i % 2 == 1 : i;… ) 

When not to use Assertions

1. Argument checking in public methods

Arguments in public methods may be provided by the user.

So, if an assertion is used to check these arguments, the conditions may fail and result in AssertionError.

Instead of using assertions, let it result in the appropriate runtime exceptions and handle these exceptions.

2. To evaluate expressions that affect the program operation

Do not call methods or evaluate exceptions that can later affect the program operation in assertion conditions.

Let us take an example of a list weekdays which contains the names of all the days in a week.

  ArrayList weekdays = new ArrayList(Arrays.asList("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" )); ArrayList weekends= new ArrayList(Arrays.asList("Sunday", "Saturday" )); assert weekdays.removeAll(weekends); 

Here, we are trying to remove elements Saturday and Sunday from the ArrayList weekdays.

यदि अभिकथन सक्षम है, तो प्रोग्राम ठीक काम करता है। हालांकि, यदि दावे अक्षम हैं, तो सूची से तत्व हटाए नहीं गए हैं। इससे प्रोग्राम की विफलता हो सकती है।

इसके बजाय, परिणाम को एक चर के साथ असाइन करें और फिर उस चर का उपयोग करें।

 ArrayList weekdays = new ArrayList(Arrays.asList("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" )); ArrayList weekends= new ArrayList(Arrays.asList("Sunday", "Saturday" )); boolean weekendsRemoved = weekdays.removeAll(weekends); assert weekendsRemoved; 

इस तरह, हम यह सुनिश्चित कर सकते हैं कि सभी सप्ताह के दिनों को हटाए जाने या अक्षम किए जाने की परवाह किए बिना सप्ताह के दिनों से हटा दिया जाए। नतीजतन, यह भविष्य में कार्यक्रम के संचालन को प्रभावित नहीं करता है।

दिलचस्प लेख...